我有一个rootViewController
,在viewDidLoad
方法中,我将另外两个ViewController2*
对象及其视图初始化为rootViewController.view
的子视图,然后我首先设置{{1} }}
然后,在v1上有一个按钮处理程序,当触摸它时,它呈现一个UINavigationController,然后在v1上触摸'dismiss'按钮调用ViewController2* controller.view.hidden = YES
。
问题是:当解雇完成时,dismissViewControllerAnimated
两个人ViewController2*
。如何使它只触发可见的viewWillAppear
,而不是隐藏的那个?
viewWillAppear
的实施:
rootViewController
ViewController2:
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.v1 = [[ViewController2 alloc] init];
self.v1.title = @"v1";
[self.view addSubview:self.v1.view];
self.v1.view.hidden = YES;
self.v2 = [[ViewController2 alloc] init];
self.v2.title = @"v2";
[self.view addSubview:self.v2.view];
UIButton * btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setTitle:@"POP" forState:UIControlStateNormal];
[btn sizeToFit];
[btn addTarget:self action:@selector(touchHandler:) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:btn];
}
- (void)touchHandler:(id)sender {
UINavigationController * nc= [[UINavigationController alloc] initWithRootViewController:[[UIViewController alloc] initWithNibName:nil bundle:nil]];
((UIViewController *)[nc.viewControllers objectAtIndex:0]).navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"dismiss" style:UIBarButtonItemStyleBordered target:self action:@selector(dismissHandler:)];
[self presentViewController:nc animated:YES completion:nil];
}
- (void) dismissHandler:(id)sender
{
[self dismissViewControllerAnimated:YES completion:nil];
}
@end
答案 0 :(得分:0)
viewWillAppear将触发你的UIViewController,即使视图控制器视图设置为hidden = YES。
如果你想阻止一些昂贵的操作发生,你可以在viewWillAppear委托方法中测试是否(self.view.hidden == YES),但要注意,如果你以后将该视图取消隐藏,那么viewWillAppear赢了然后开火了。
答案 1 :(得分:0)
简单,调用这些方法的原因是因为viewController的视图是主窗口视图层次结构的一部分。这意味着它有一个superview,它有一个superview,有一个superview等等,直到superview是主窗口。 您应该在超级视图中添加和删除它们,而不是隐藏和取消隐藏viewController视图。另外,要确保在正确的时间正确调用viewWillAppear和viewDidAppear,请查看ViewController Containment: http://www.cocoanetics.com/2012/04/containing-viewcontrollers/