弹出到不支持当前方向的视图控制器时,未调用 UINavigationControllerDelegate 方法。
我有一个 UINavigationController 作为我的应用程序的根视图控制器(我的故事板中的初始视图控制器)。
假设我用这个来推送一个 ViewController A 来定位:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
因此我们不支持任何横向模式。最重要的是,让我们用代码推送另一个 ViewController:
@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
现在,如果我的 iPhone 是纵向的,我通过一些触摸事件将视图控制器 B 推到 A 上,然后触摸导航栏中的“后退”按钮,一切都很好,并且调用了委托方法(我在那里做了一些事情)。
但是,如果当我查看视图 B 时,我旋转到横向,然后点击后退按钮,则不会调用这些委托方法!如果有时调用方法但不调用其他方法,那么为 UINavigationController 设置委托有什么意义?我肯定做错了什么。
我尝试将委托放在其他类中,例如 AppDelegate 或我的 MainView,没有任何变化。
有任何想法吗 ?
演示代码在这里: git://github.com/malaba/NavBarTest.git
从一个基本的主从模板,如上修改。
如何表明我的观点?这是一步一步:
- 在主视图中添加一些行(+ 右上角)
- 然后触摸一条线进入详细信息视图
- 请参阅显示的登录输出
- 点击返回按钮(标记为“Master”),再次显示日志。
现在尝试在详细信息视图中旋转 iPhone(或模拟器),然后“返回”并看到没有显示日志?!