问题
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...
回答1:
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:
.
回答2:
It is possible to do it in Interface Builder at least in Xcode 6.
- Have a class that confirms to
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
classCtrl-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.
回答3:
I am very new to Xcode, so this explanation may be completely wrong and not your question at all. If you are trying to link a view to a view controller, have you tried selecting the view and then setting a custom class to control it under the identity inspector? Again, I don't quite understand your question, but I figured I'd try to help anyways.
来源:https://stackoverflow.com/questions/21501286/is-it-possible-to-ctrl-drag-in-storyboard-to-set-a-uinavigationcontrollerdelegat