UINavigationControllerDelegate methods are not called (demo code on github)

UINavigationControllerDelegate methods that are not called when entering the view controller that do not support the current orientation.

I have a UINavigationController as the root view controller of my application (the initial view controller on my storyboard).

Let's say I click ViewController A with this for orientation:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

Therefore, we do not support landscape mode. Also, let me click another ViewController with the code:

@interface B : UIViewController <UINavigationControllerDelegate>
@end

@implementation B
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;   // any orientation supported
}

- (void)viewDidLoad {
    [super viewDidLoad];

    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    UINavigationController *nc = (UINavigationController*)appDelegate.window.rootViewController;
    nc.delegate = self;
}

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    // not always called...
}

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    // not always called...
}
@end

Now, if I have my iPhone in portrait, and I press the view controller B over A, after some touch event, then touch the back button in the navigation bar, everything is fine and the delegate methods are called (I do some things there).

, B, , "", ! UINavigationController, , ? , - .

, AppDelegate MainView, .

?

- : git://github.com/malaba/NavBarTest.git

Master-Detail, , .

? :

  • ( )
  • , .
  • .
  • "" ( "" ), .

iPhone ( Simulator) , .?

+5
2

Kaspar , Obj-C.

.h:

@interface ProperNavigationController : UINavigationController

@end

@interface ProperNavigationControllerDelegate : NSObject <UINavigationControllerDelegate>
@property (assign, nonatomic) BOOL wasCalled;
@end

.m:

#import "ProperNavigationController.h"

@interface ProperNavigationController ()
@property (strong, nonatomic) id<UINavigationControllerDelegate> oldDelegate;
@property (strong, nonatomic) ProperNavigationControllerDelegate *myDelegate;
@end

@implementation ProperNavigationController
@synthesize oldDelegate = _oldDelegate;
@synthesize myDelegate = _myDelegate;

- (void)viewDidLoad {
    [super viewDidLoad];

    self.oldDelegate = self.delegate;
    self.myDelegate = [ProperNavigationControllerDelegate new];
    self.delegate = self.myDelegate;
}

- (UIViewController *)popViewControllerAnimated:(BOOL)animated {
    self.myDelegate.wasCalled = FALSE;

    UIViewController *vc = [super popViewControllerAnimated:animated];

    if (!self.myDelegate.wasCalled) {
        // if iOS did not call the delegate handler then we must do it
        [self.myDelegate navigationController:self willShowViewController:self.topViewController animated:animated];
        [self.myDelegate navigationController:self didShowViewController:self.topViewController animated:animated];
    }

    return vc;
}

@end

@implementation ProperNavigationControllerDelegate
@synthesize wasCalled = _wasCalled;     // flag that we use to track whether iOS calls the handlers or we have to 

- (id)init {
    if (self = [super init]) {
       _wasCalled = FALSE; 
    }
    return self;
}

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    ProperNavigationController *nc = (ProperNavigationController *)navigationController;
    [nc.oldDelegate navigationController:navigationController willShowViewController:viewController animated:animated];
    self.wasCalled = TRUE;  // signal that we have been called
}

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    ProperNavigationController *nc = (ProperNavigationController *)navigationController;
    [nc.oldDelegate navigationController:navigationController didShowViewController:viewController animated:animated];
}

@end

.

? ?

+1

, , : http://www.hanspinckaers.com/custom-action-on-back-button-uinavigationcontroller

NavigationController, TopViewController , IOS , (monotouch)

  • SomeTopViewController ViewWillDisappear
  • WillShowViewController viewController: TopViewController
  • SomeTopViewController ViewDidDisappear
  • DidShowViewController viewController: TopViewController

NavigationController TopViewController , NavigationController not, . , :

  • SomeTopViewController ViewWillDisappear
  • SomeTopViewController ViewDidDisappear

ios, NavigationController "PopViewControllerAnimated:

public class MyNavigationControllerDelegate : UINavigationControllerDelegate {
  public MyNavigationControllerDelegate( ) : base() {}

  public bool WasCalled {get;set;}  // flag that we use to track whether iOS calls the handlers or we have to 

  public override void WillShowViewController( UINavigationController navigationController, UIViewController viewController, bool animated ) {
    Console.WriteLine( "WillShowViewController viewController: {0}", viewController.GetType() );
    WasCalled = true;  // signal that we have been called
    //.….. do your stuff
  }

  public override void DidShowViewController( UINavigationController navigationController, UIViewController viewController, bool animated ) {
    Console.WriteLine( "DidShowViewController viewController: {0}", viewController.GetType() );
    //.….. do your stuff
  }
}

public partial class MyNavigationController : UINavigationController {
  MyNavigationControllerDelegate navigationControllerDelegate;

  public override void ViewDidLoad() {
    base.ViewDidLoad();
    navigationControllerDelegate = new MyNavigationControllerDelegate( viewSelectionControl );
    Delegate = navigationControllerDelegate;
  }

public override UIViewController PopViewControllerAnimated( bool animated ) {
  Console.WriteLine( "PopViewControllerAnimated TopViewController.GetType: {0}", TopViewController.GetType() );
  navigationControllerDelegate.WasCalled = false;   // reset flag before we start the popsequence

  UIViewController ctrl = base.PopViewControllerAnimated( animated );

  AppDelegate.MainWindow.BeginInvokeOnMainThread( delegate {
    if( !navigationControllerDelegate.WasCalled )  {   // if iOS did not call the delegate handler then we must do it
      Delegate.WillShowViewController( this, TopViewController, animated );
      navigationControllerDelegate.WasCalled = false;  // reset flag to be used in the next DidShowViewController step of the popsequence
      }
  } );

  Thread t = new Thread( () => RunPop(animated) );
  tt.Start();

  return ctrl;
}

void RunPop(bool animated) {
  Thread.Sleep( 500 );
  AppDelegate.MainWindow.BeginInvokeOnMainThread( delegate {
    if( !navigationControllerDelegate.WasCalled ) {  // if iOS did not call the delegate handler then we must do it
      Delegate.DidShowViewController(this,TopViewController,animated);
    }
  } );
}

}

+2

All Articles