Error with top view controller

413 Views Asked by At

I have one difficulty with the root view controller.

enter image description here Picture(1)

Within the below code and the picture 1, everything work fine. But within picture 2 enter image description here Picture (2), I got an error said that

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

So please help me how to insert a view controller in front of the navigation controller?

1

There are 1 best solutions below

3
On

You can get a reference to the navigation controller through the childViewControllers property of the container 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;
}