2

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?

jose920405
  • 7,982
  • 6
  • 45
  • 71
borin prak
  • 23
  • 6
  • Your `UINavigationController` is actually `BannerViewController`. You have to check this line `UINavigationController *navController = tabBarController.viewControllers[0];` – jose920405 Feb 26 '16 at 16:16

1 Answers1

0

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;
}
pbasdf
  • 21,386
  • 4
  • 43
  • 75
  • After applying your code, I got another error saying: "Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArray0 objectAtIndex:]: index 0 beyond bounds for empty NSArray'" What should I do? – borin prak Feb 26 '16 at 16:58
  • @borinprak Hmm... seems like the container VC has not yet instantiated the child view controller. Let me test some ideas..... – pbasdf Feb 26 '16 at 17:09
  • @borinprak To force the containerVC to load its view, and therefore the childVC, just access its view - I'll amend code accordingly. – pbasdf Feb 26 '16 at 17:30