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

2019-04-09 06:54发布

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

标签: xcode xcode5
3条回答
【Aperson】
2楼-- · 2019-04-09 07:32

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.

查看更多
一夜七次
3楼-- · 2019-04-09 07:46

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.

查看更多
闹够了就滚
4楼-- · 2019-04-09 07:48

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

查看更多
登录 后发表回答