我有一个rootViewController
,在它的方法中,我将另外两个对象和它们的视图viewDidLoad
初始化为的子视图,然后我先设置。ViewController2*
rootViewController.view
ViewController2* controller.view.hidden = YES
然后,在 v1 上有一个按钮处理程序,当触摸它时,它会呈现一个 UINavigationController,然后在 v1 上触摸“关闭”按钮调用dismissViewControllerAnimated
。
问题是:当dismiss完成时,两个ViewController2*
fire 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
视图控制器2:
@implementation ViewController2
- (void)viewWillAppear:(BOOL)animated
{
NSLog(@"%@",self.title);
}
@end