McKrassy
McKrassy

Reputation: 991

Problem implementing UINavigationControllerDelegate

I may have some misunderstanding regarding the use of the UINavigationControllerDelegate protocol. Here is my situation:

I have a ViewController, let's call it, BViewController that may display a PopoverViewController. BViewController is the second ViewController in a NavigationContoller's stack, after AViewController. I need to dismiss the PopoverViewController when the user hits a button in BViewController and the app takes us back to the previous view--AViewController.

To do that, I have implemented the following in BViewController

- (void)viewWillDisappear:(BOOL)animated {
    NSLog(@"BViewController will disappear");
    // Check whether the popoverViewController is visible
    if (self.popoverController.popoverVisible==YES) {
        [self.popoverController dismissPopoverAnimated:NO];
    }
}

However, that is not being called directly by the framework as BViewController is inside a NavigationController. Hence, I register a UINavigationControllerDelegate with my NavigationController and implement the following two methods:

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    // Pass the message on to the viewController in question
    [viewController viewWillAppear:animated];
}

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    // Pass the message on to the viewController in question
    [viewController viewWillDisappear:animated];
}

However, it seems that the passed in viewController parameter in both methods is the one that is about to be shown. I would have expected that the second method gives me access to the one that is about to disappear. So, when the user hits aforementioned button viewWillDisappear gets called on AViewController (which is about to be shown) and not on BViewController (which is about to disappear). Does that sound right? The apple documentation refers in both cases to

The view controller whose view and navigation item properties are being shown.

...which is not quite clear, I think. Thank you for some help, guys.

Upvotes: 0

Views: 1815

Answers (2)

Dirk P
Dirk P

Reputation: 1

I hook in my own protocol, which will know about the TO and FROM sides:

NavigationControllerDelegate.h:

@protocol NavigationControllerDelegate <NSObject>
@required
-(void) navigationController: (UINavigationController*) navController 
  willMoveFromViewController: (UIViewController*) from 
        toViewController: (UIViewController*) to;
@end

Instead of the regular UINavigationViewController, I then use a little helper class which keeps track of the view controllers:

NavigationHandler.h:

@interface NavigationHandler : NSObject <UINavigationControllerDelegate> {
  NSMutableArray* m_viewControllers;
}

In my app delegate, I create one of these objects and set it as the delegate of the navigation controller:

...
m_navigationHandler = [[NavigationHandler alloc] init];
navigationController = [[UINavigationController alloc] initWithRootViewController: mainMenuViewController];
navigationController.delegate = m_navigationHandler;
...

And from then on its a simple case of comparing my own list of view controllers with what the navigation controller has:

NavigationHandler.m

#import "NavigationHandler.h"
#import "NavigationControllerDelegate.h"

@implementation NavigationHandler

-(id) init {
  if ((self = [super init])) {
    m_viewControllers = [[NSMutableArray alloc] init];
  }
  return self;
}

-(void) dealloc {
  [m_viewControllers release];
  [super dealloc];
}

- (void)navigationController:(UINavigationController *)navController 
  willShowViewController:(UIViewController *)viewController 
                animated:(BOOL)animated {

  // Find out which viewControllers are disappearing and appearing

  UIViewController* appearingViewController = nil;
  UIViewController* disappearingViewController = nil;

  if ([m_viewControllers count] < [navController.viewControllers count]) {

    // pushing
    if ([m_viewControllers count] > 0) {
      disappearingViewController = [m_viewControllers lastObject];
    }
    appearingViewController = viewController;
    [m_viewControllers addObject: viewController];

  } else if ([m_viewControllers count] > [navController.viewControllers count]) {

    // popping
    disappearingViewController = [m_viewControllers lastObject];
    appearingViewController = viewController;
    [m_viewControllers removeLastObject];

  } else {
    return;
  }

  // Tell the view that will disappear
  if (disappearingViewController != nil) {
    if ([disappearingViewController conformsToProtocol: @protocol(NavigationControllerDelegate)]) {
      if ([disappearingViewController respondsToSelector: @selector(navigationController:willMoveFromViewController:toViewController:)]) {
        UIViewController<NavigationControllerDelegate>* vcDelegate = (UIViewController<NavigationControllerDelegate>*)disappearingViewController;
        [vcDelegate navigationController: navController willMoveFromViewController: disappearingViewController toViewController: appearingViewController];
      }
    }
  }

  // Tell the view that will appear
  if ([appearingViewController conformsToProtocol: @protocol(NavigationControllerDelegate)]) {
    if ([appearingViewController respondsToSelector:@selector(navigationController:willMoveFromViewController:toViewController:)]) {
      UIViewController<NavigationControllerDelegate>* vcDelegate = (UIViewController<NavigationControllerDelegate>*)appearingViewController;
      [vcDelegate navigationController: navController willMoveFromViewController: disappearingViewController toViewController: appearingViewController];
    }
  }
}   

@end

Upvotes: 0

Chris
Chris

Reputation: 748

The two delegate method are both called for the same action (showing a view controller). The navigationController: willShowViewController:animated: is called before the new view controller is visible in the gui. The navigationController:navigationController didShowViewController:animated: is called after the new view controller is shown.

You will find this pattern in a lot of delegate protocols from apple. Unfortunately you do not have a delegate method in the NavigationViewController which tells you if the action was a pop or push.

Upvotes: 2

Related Questions