Monday, March 14, 2011

How to Copy Current Toolbar to Next NavigationController

Your application is based on idea of lists, where you can navigate deeper into sublists and then back again. There is a common set of actions, which you want to launch from toolbar at the bottom of each screen.

Sounds easy, except you need to define the toolbar separately for each UIViewController while navigating deeper into lists. That should be easier, right? Try this:
MyViewController *myVC = [[MyViewController alloc] initWithNibName:@"MyView" bundle:nil];
myVC.toolbarItems = self.toolbarItems;
[self.navigationController pushViewController:myVC animated:YES];
[myVC release];
The good news is that now each view does have the same toolbar - with just one line of code!

The bad news is that it really is the same toolbar, including action callbacks. For truly global action like "Open Settings" it's fine, but for view specific action like "Search This Sublist" you need to figure out some way to forward the action from initial callback location to current viewController. This could be done e.g. via notifications:
[[NSNotificationCenter defaultCenter]
 postNotificationName:MyRefreshNotification
 object:nil];

[[NSNotificationCenter defaultCenter]
 addObserver:self
 selector:@selector(myRefreshCallback)
 name:MyRefreshNotification
 object:nil];
You have to choose between a) creating separate toolbar for each viewController or 2) handling all callbacks in one place and possibly forwarding them to currently active view.

No comments:

Post a Comment