顶视图控制器出错

时间:2016-02-26 15:36:02

标签: ios uiviewcontroller uinavigationcontroller uitabbar rootviewcontroller

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

enter image description here图片(1)

在下面的代码和图片1中,一切正常。 但是在图片2 enter image description here图片(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;
}

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

1 个答案:

答案 0 :(得分:0)

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

- (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;
}