Isaac Overacker
Isaac Overacker

Reputation: 1525

NSNotification not handled by subview contained in a UINavigationController

In my AppDelegate, I'm instantiating a UINavigationController and adding my "main" view (let's call it MainViewController) as the root view controller for the navigation controller. The navigation controller is then added as a subview to the app delegate's window and the window is made visible. I.e.,

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
   // viewController is a property of the app delegate
   viewController = [[MainViewController alloc] init];
   viewController.context = [self managedObjectContext];

   UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController];

   [self.window addSubview:navController.view];
   [self.window makeKeyAndVisible];
   return YES;
}

MainViewController adds observers for some NSNotifications that are posted by a subview of a view controller programmatically instantiated and added as a subview of MainViewController. So,

Once I added the UINavigationController to the mix, MainViewController no longer properly handles the notification posted by SubView. If I remove the UINavigationController and simply add MainViewController as a subview of the AppDelegate's window, the notifications are handled properly.

I tried adding an observer to the AppDelegate for one of the notifications posted by SubView and it handles it properly, and while handling that notification, the MainViewController is not nil.

Any guidance would be greatly appreciated. Please let me know if you'd like any clarification.

Upvotes: 0

Views: 324

Answers (1)

Isaac Overacker
Isaac Overacker

Reputation: 1525

Ok, a couple of things were happening here:

  1. viewController was not actually a property on my app delegate. Making it a property caused the NSNotification to be handled again.
  2. After that was sorted out, the code in the selector being triggered by the NSNotification wasn't working properly--it was supposed to push a new view into MainViewController's navigationController, which was nil. Creating a property in the app delegate for the UINavigationController fixed this issue, too.

Upvotes: 0

Related Questions