UINavigationControllerDelegate methods not called when poping to a view controller that don’t support the current orientation.
I have a UINavigationController as root view controller of my app (initial view controller in my Storyboard).
Let say I push a ViewController A with this for orientation:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
Hence we don’t support any landscape mode. On top of that let’s push another ViewController with 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 push view controller B on top of A, via some touch event, then touch the “back” button in the navbar, everything is fine and the delegate methods are called (I do some stuff there).
But if, when I’m viewing view B, I rotate to landscape, and then hit the back button, those delegate methods are not called!! What’s the point of setting a delegate for the UINavigationController if the methods are called sometimes but not other ? Surely I’m doing something wrong.
I try to put the delegate in other class, like AppDelegate or my MainView, nothing change.
Any ideas ?
demo code here: git://github.com/malaba/NavBarTest.git
From a basic Master-Detail template, modified just as above.
How to show my point ? Here is the step-by-step:
- Add some line in Master View (the + top right)
- then touch a line to go to details view
- See Log in output showing up
- Hit back button (labeled “Master”), again log showing up.
Now try to rotate the iPhone (or Simulator) in details view, then go “back” and see no log showing up?!
by Kaspar answer, here is my code in Obj-C.
.h:
.m:
it work.
What do you think ? And should we fill a bug report ?