mal*_*aba 5 cocoa-touch delegates uinavigationcontroller
弹出到不支持当前方向的视图控制器时未调用的UINavigationControllerDelegate方法.
我有一个UINavigationController作为我的应用程序的根视图控制器(我的Storyboard中的初始视图控制器).
假设我将ViewController A推送到方向:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
Run Code Online (Sandbox Code Playgroud)
因此,我们不支持任何横向模式.最重要的是,让我们用另一个代码推送另一个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
Run Code Online (Sandbox Code Playgroud)
现在,如果我有我的iPhone在纵向和我推A的顶视图控制器B,通过一些触摸事件,然后点击导航条上的"返回"按钮,一切都很好,并委托方法被调用(我做一些东西有).
但是,如果,当我正在查看视图B时,我旋转到横向,然后点击后退按钮,那些委托方法不会被调用!! 如果有时调用方法而不调用其他方法,那么为UINavigationController设置委托有什么意义呢?当然,我做错了什么.
我尝试将委托放在其他类中,如AppDelegate或我的MainView,没有任何变化.
有任何想法吗 ?
演示代码在这里: git://github.com/malaba/NavBarTest.git
从基本的Master-Detail模板,如上修改.
如何表明我的观点?这是一步一步:
现在尝试在详细信息视图中旋转iPhone(或模拟器),然后"返回"并看到没有日志显示?!
由 Kaspar 回答,这是我在 Obj-C 中的代码。
。H:
@interface ProperNavigationController : UINavigationController
@end
@interface ProperNavigationControllerDelegate : NSObject <UINavigationControllerDelegate>
@property (assign, nonatomic) BOOL wasCalled;
@end
Run Code Online (Sandbox Code Playgroud)
.米:
#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
Run Code Online (Sandbox Code Playgroud)
这行得通。
你怎么认为 ?我们应该填写错误报告吗?