In Xcode 5.0.2 I've created an app for iPhone with a navigation controller and 3 further view controllers (here fullscreen):
I'd like the LoginViewController (with the "Login" title in the above screenshot) be the delegate for the navigation controller, so that its method
- (void)navigationController:(UINavigationController *)navigationController
didShowViewController:(UIViewController *)viewController
animated:(BOOL)animated
is called - so I have added the following code to its viewDidLoad
:
UINavigationController *nc = self.navigationController;
[nc setDelegate:self];
This works, but I wonder if setting the delegate for the navigation controller is also possible in the Interface Builder?
I've tried ctrl-dragging from the yellow cube sign of the navigation controller to the LoginViewController scene and vice versa - but I'm only offered a popup to create a segue...
It is possible to do it in Interface Builder at least in Xcode 6.
UINavigationControllerDelegate
In Interface Builder, from the Object library drag Object into your Navigation Controller (either into Document Outline or the toolbar above the Navigation Controller in Storyboard)
Set Object's Class in Identity inspector to your UINavigationControllerDelegate
class
Ctrl-drag from the Navigation Controller object to your delegate object
From the popup menu select "delegate"
That's all. Now your object will be Navigation Controller's delegate.
EDIT: this answer might be outdated as of Dec 2015. This answer is probably more accurate for XCode 6 and newer.
Long long ago, before storyboards existed, setting delegates was possible using outlets (although I don't remember if it was possible for UINavigationController, I only remember that I did it with UITableView). You would just right-click on Navigation Controller and drag from the outlet to the target:
However, according to this answer, it's not possible anymore. You can do it programatically in viewDidLoad
as you did, or using prepareForSegue:
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With