我有一個rootViewController
,在它的viewDidLoad
方法,我初始化另外兩個ViewController2*
對象及其作爲rootViewController.view
子視圖的意見,然後我設置第一ViewController2* controller.view.hidden = YES
。如何中斷viewWillAppear中層次
然後,在v1上有一個按鈕處理程序,當它觸摸它時,它會呈現一個UINavigationController,然後在v1上觸摸'取消'按鈕調用dismissViewControllerAnimated
。
現在的問題是:當解僱完成後,兩個ViewController2*
火viewWillAppear
。如何使它只在可見的一個上觸發viewWillAppear
,但不在隱藏的那個上?
的rootViewController
的實現:
@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
ViewController2:
@implementation ViewController2
- (void)viewWillAppear:(BOOL)animated
{
NSLog(@"%@",self.title);
}
@end
是的,但這種情況很簡單,我可以測試是否隱藏= YES。但是,如果我有一個更復雜的情況:rootViewController - > ViewController2 - > ViewController3,ViewController2.view.hidden = YES,當關閉某些模式的viewController時,ViewController3的viewWillAppear仍然會觸發。我怎樣才能防止它發生? –