Objective-C页面消失或出现时,判断是pop还是push操作
当前的controller消失的时候回先后执行
- (void)viewWillDisappear:(BOOL)animated;
-(void)viewDidDisappear:(BOOL)animated;
这两个方法;在这两个方法中进行判断消失的方式即可:
- (void)viewWillDisappear:(BOOL)animated {
NSArray *viewControllers = self.navigationController.viewControllers;//获取当前的视图控制其
if (viewControllers.count > 1 && [viewControllers objectAtIndex:viewControllers.count-2] == self) {
//当前视图控制器在栈中,故为push操作
NSLog(@"push");
} else if ([viewControllers indexOfObject:self] == NSNotFound) {
//当前视图控制器不在栈中,故为pop操作
NSLog(@"pop");
}
}
页面出现时会先后执行:
-(void)viewWillAppear:(BOOL)animated
-(void)viewDidAppear:(BOOL)animated
这两个方法;
如果是push出来的页面则还会执行:
- (void)viewDidLoad
所以可在viewdidload里加个 isPush = YES布尔值,然后在Appear中根据isPush判断是push还是pop:
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
if (_isPush) {
//push
}else{
//pop
}
}
别忘了页面消失时置isPush为NO:
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
_isPush = NO;
}
网友评论