2

我对根视图控制器有一个困难。

在此处输入图片描述图片(1)

在下面的代码和图片 1 中,一切正常。但是在图 2中在这里输入图像描述图(2),我得到一个错误说

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[BannerViewController topViewController]: unrecognized selector sent to instance 0x7feb9b64dbc0'

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
    UINavigationController *navController = tabBarController.viewControllers[0];

    ItemListTableViewController *itemList = (ItemListTableViewController *)navController.topViewController;
    itemList.managedObjectContext = self.managedObjectContext;

    return YES;
}

所以请帮我如何在导航控制器前面插入一个视图控制器?

4

1 回答 1

0

可以通过childViewControllers容器VC的属性获取导航控制器的引用:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
    UIViewController *containerVC = tabBarController.viewControllers[0];
    UIView *containerVCview = containerVC.view;
    UINavigationController *navController = containerVC.childViewControllers[0];

    ItemListTableViewController *itemList = (ItemListTableViewController *)navController.topViewController;
    itemList.managedObjectContext = self.managedObjectContext;

    return YES;
}
于 2016-02-26T16:29:41.927 回答