How to set a delegate for a CustomToolbar in the rootViewController when using a NavigationController setup with initWithNavigationBarClass:toolbarClass:
In my code I am doing the following:
RootViewController *rootViewController = [[RootViewController alloc] initWithClassName:kFTPostClassKey];
UINavigationController *navController = [[UINavigationController alloc] initWithNavigationBarClass:[CustomNavigationBar class] toolbarClass:[CustomToolbar class]];
[navController setViewControllers:@[rootViewController] animated:NO];
// Present the Home View Controller
[self presentViewController:navController animated:YES completion:NULL];
In the 'RootViewController' I am trying to set the delegate for the toolbar in order to detect when an item is tapped.
[self.navigationController.toolbar setDelegate:self];
When I do this I get an exception:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Cannot manually set the delegate on a UIToolbar managed by a controller.'
I looked at the documentation and learned that you can not set the delegate when the toolbar is being managed by the UINavigationController. My question is this: How can I detect clicks on the CustomToolbar in my RootViewController if I can not set the delegate?
I would also appreciate some deeper explanation, as I am new and not sure how this is all working.
My guess is that in order to work, the navigationController
needs to be the navigationBar
delegate
. That's why you can not set the navigationBar
delegate
when it is used in a navigationController
.
As you use a subclass of navigationBar
, you can try to have a subprotocol of the UINavigationBarDelegate
in which you add a navigationBarWasSelected method.
In a new UINavigationController
subclass, you implement this method to let the rootViewController
now when the navigation is clicked.
Or, in your custom navigation bar you can send a NSNotification
in the NSNotificationCenter
, and catch it in your RootViewController
.