Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to ctrl-drag in Storyboard to set a UINavigationControllerDelegate?

Tags:

xcode

xcode5

In Xcode 5.0.2 I've created an app for iPhone with a navigation controller and 3 further view controllers (here fullscreen):

xcode screenshot

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...

like image 573
Alexander Farber Avatar asked Feb 01 '14 17:02

Alexander Farber


2 Answers

It is possible to do it in Interface Builder at least in Xcode 6.

  1. Have a class that confirms to UINavigationControllerDelegate
  2. 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) enter image description here

  3. Set Object's Class in Identity inspector to your UINavigationControllerDelegate class enter image description here

  4. Ctrl-drag from the Navigation Controller object to your delegate object enter image description here

  5. From the popup menu select "delegate" enter image description here

That's all. Now your object will be Navigation Controller's delegate.

like image 175
Alex Avatar answered Oct 02 '22 20:10

Alex


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:

outlets

However, according to this answer, it's not possible anymore. You can do it programatically in viewDidLoad as you did, or using prepareForSegue:.

like image 20
Michał Ciuba Avatar answered Oct 02 '22 20:10

Michał Ciuba