0

How to delegate methods actually get called?

Like lets say I add this to my UIViewController.m:

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    // code
}

Does the method get called when the view controller appears? How does delegation work?

cdub
  • 24,555
  • 57
  • 174
  • 303

3 Answers3

2

If you set your object as the delegate of the navigation controller, the navigation controller will call this method when a view controller is about to appear.

For delegate method implementation details, see this question and Concepts in Objecctive-C Programming: Delegates and Data Sources.

Community
  • 1
  • 1
jtbandes
  • 115,675
  • 35
  • 233
  • 266
2

According to the docs, the delegate is called just before the navigation controller displays a viewController’s view and navigationItem properties.

But, it appears that your question is, "how?".

In your Storyboard or somewhere in code, the UINavigationController is set up. Wherever that is, it has a delegate property (a variable). That property is set to some object that implements the UINavigationControllerDelegate protocol.

For example:

MySpecialViewController *myViewController = [[MySpecialViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:myViewController];
navigationController.delegate = self;

Now, whenever that navigationController is about to present a view controller, the navigationController:willShowViewController:animated: delegate method will be called on your object.

Troy
  • 5,319
  • 1
  • 35
  • 41
  • so if I have the navigationController that is sspecified like above and set like so: self.window.rootViewController = navigationController; then do all my view controllers have the navigation controller as a parent to use? – cdub Jul 23 '14 at 06:21
  • Sort of. I suggest that you read through Apple's UINavigationController documentation. It's quite illustrative. Basically, you call [navigationController pushViewController:myController animated:YES] to add it to the stack and [navigationController popViewControllerAnimated:YES] to remove one. That causes the familiar iPhone slide to the right animation with a back button. – Troy Jul 24 '14 at 05:25
1

Well it's probably something like this inside UINavigationController class:

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
   [self.delegate navigationController:self willShowViewController:viewController animated:animated];

   //do stuff to actually push the view controller
}

If the delegate is nil nothing happens because messages to nil make no effect, but if you set the delegate that method is invoked. I'm not sure if it happens directly inside pushViewController: method, but that doesn't really matter. Whenever the navigation controller is about to show next view controller it sends a message to its delegate

Andrey Chernukha
  • 21,488
  • 17
  • 97
  • 161