ios - UINavigationController 为 NULL

标签 ios uinavigationcontroller uitabbarcontroller

我已经仔细阅读了 Apple 文档以及类似的 Stack Overflow 问题,但我仍然不明白为什么在使用标签栏时我的 navigationController 为 null。我正在尝试从代码构建大部分应用程序,而不是使用 XIB 来插入导航 Controller 。

在调试时,我大大简化了我的应用程序,简化为两个选项卡。一个选项卡包含一个表格 View ,当触摸一行时,我希望出现一个详细信息页面(来自 XIB)。应该很简单。我在尝试推送详细 View 时发现 self.navigationController 的值为 NULL,当然它随后无法正常工作。我完全采用了标签栏,它在单个 View (表格 View )中工作正常。在这个例子中,self.navigationController 有一个值。

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // With Tab Bars

    self.tabBarController = [[UITabBarController alloc] init];

    ViewController *vc1 = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    vc1.tabBarItem.title = @"Words";
    vc1.tabBarItem.image = [UIImage imageNamed:@"tab_feed.png"];

    TextTableViewController *vc2 = [[TextTableViewController alloc] init];
    vc2.tabBarItem.title = @"Text";
    vc2.tabBarItem.image = [UIImage imageNamed:@"tab_live.png"];

    UINavigationController* navController = [[UINavigationController alloc] initWithRootViewController:vc1];                             
    NSArray* controllers = [NSArray arrayWithObjects:vc2, navController, nil];
    tabBarController.viewControllers = controllers;
    tabBarController.delegate = self;

    window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    window.rootViewController = self.tabBarController;
    [window makeKeyAndVisible];
    return YES;  
}

来自 TextTableViewController.m

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{ 
    TextViewController *detailViewController = [[TextViewController alloc] initWithNibName:@"TextViewController" bundle:nil];  
    Text *text = [[Text alloc] init];
    text = [textArray objectAtIndex:indexPath.row];

    detailViewController.TextID = text.textID;
    NSLog(@"Nav Controller: %@",self.navigationController);
    [self.navigationController pushViewController:detailViewController animated:YES];
    NSLog(@"pushed"); 
}

我还有两个与此问题相关的问题。 (1) 这行的目的是什么。它是进还是出似乎没有什么区别,并且在 Apple 示例中没有出现。

tabBarController.delegate = self;

(2) 创建选项卡数组时,其中一个 View 成为导航 Controller 。它是哪个选项卡是否重要,或者这应该是一个完全与任何选项卡无关且不可见的不同 View 。这是问题所在吗?

最佳答案

在回答关于选项卡栏 Controller 委托(delegate)的问题 (1) 时,请参阅 UITabBarControllerDelegate 协议(protocol)引用。对于选项卡栏 Controller 的基本功能,您无需费心使用委托(delegate)。

但假设您想在用户更改选项卡时做一些特别的事情——例如,保存文档或将界面元素重置为默认值。你可以让你的一个类,也许是你的应用程序委托(delegate)或另一个 Controller 类,符合 UITabBarControllerDelegate 协议(protocol)并实现 tabBarController:didSelectViewController:

在您的“答案”中,您询问每个选项卡是否需要自己的 UINavigation Controller 。这是绝对正确的。基本上,每个选项卡都是一个完全独立的层次结构,因此您需要在每个需要一个的选项卡中使用一个单独的 UINavigation Controller 。

这也应该暗示您在原始帖子中的问题 (2) 的答案。您需要将导航 Controller 添加到需要它的特定选项卡。

关于ios - UINavigationController 为 NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11385836/

相关文章:

ios - 无法从此设备复制符号

iphone - 单击 "back"按钮时 UINavigationController 应用程序崩溃

ios - 选项卡栏 Controller - 显示不是选项卡之一的 View Controller

ios - 如何将导航 Controller 放入标签栏 Controller 中?

ios - 在项目选择上调用委托(delegate)方法

ios - 在 iOS 和 OS X 之间共享应用内购买?

ios - 使用 NSFetchedResultsController 锚定一行

cocoa-touch - UINavigationController 上的自定义 titleView 动画不正确

ios - Swift 中一个类的多个实例

swift - "Can' t add self as subview"crash (with sample code) 当 View 未将自身添加到其 subview 时