tags:

views:

875

answers:

2

Using a UITabBar, I have 4 sibling views (one per tab item). When the app loads, the first tab item and view are visible. That first view has an IBAction that posts an NSNotification. Each of the other three views have observers for the notification but they cannot "hear" the notification until they are first made visible by touching the tab bar item.

Is is possible to post an NSNotification to a sibling view's NSNotification observer before the sibling is activated or a way to load the sibling views in a manner that they can observer notifications without first activating them?

+2  A: 

The sibling views (or, perhaps more accurately, cousin views :) are probably not receiving the notifications because they haven't been instantiated yet. Normally, a view controller instantiates its view (and subviews) when it is first displayed.

What you probably want to do is to have the view controllers handle the notification rather than the subview. The view controllers are instantiated when the tab bar is set up, so they should be ready to receive notifications right away.

You can't really forward the notification from the view controller to the subview, since, for the same reason, you'll be trying to message a view that hasn't been instantiated yet. What you should do is keep track of the state in the view controller, then set up the subviews appropriately in viewDidLoad or loadView (depending on if you're using a NIB or not).

Daniel Dickison
A: 

SOLVED --

In the application delegate implementation file:

  • (void)applicationDidFinishLaunching:(UIApplication *)application {

    [window addSubview:tabBarController.view];

    self.tabBarController.selectedViewController = [self.tabBarController.viewControllers objectAtIndex:2]; self.tabBarController.selectedViewController = [self.tabBarController.viewControllers objectAtIndex:1]; self.tabBarController.selectedViewController = [self.tabBarController.viewControllers objectAtIndex:0]; }

This loads all (4) view controllers and they all immediately listen to NSNotifications.

If you're still listening to notifications in the actual views (rather than view controllers), note that they'll get released when you hit a low-memory warning.
Daniel Dickison