怎么判断viewcontroller是Push还是Pop?

Author Avatar
新风作浪 10月 23, 2015
  • 在其它设备中阅读本文章

一般情况下判断viewcontroller在返回时需要push还是pop都会在.h文件带一个参数进来。如何让容器自己判断?

方法一

NSArray* views = [self.navigationController viewControllers];
BOOL variable = [views containsObject: self];
if (variable) {
    [self.navigationController popViewControllerAnimated:YES];
}
else {
    [self dismissViewControllerAnimated:YES completion:^{}];
}

方法二

if (self.navigationController != nil) {
    [self.navigationController popViewControllerAnimated:YES];
}else{
    [self dismissViewControllerAnimated:YES completion:^{}];
}

参考:http://stackoverflow.com/questions/16562546/how-to-check-whether-the-viewcontroller-is-popped-or-pushed-without-using-the-bo