当我们自定义继承UIViewController视图控制器的基类视图控制器时,会新增返回到上级的视图控制器方法,于是就要区分,返回时使用 pop 的方法,还是使用dismiss 的方法了。
方法1
[html] view plain copy 在CODE上查看代码片派生到我的代码片
- (void)backPreviousController
{
if (self.presentingViewController)
{
[self dismissViewControllerAnimated:YES completion:nil];
}
else
{
[self.navigationController popViewControllerAnimated:YES];
}
}
方法2
[objc] view plain copy 在CODE上查看代码片派生到我的代码片
- (void)backPreviousController
{
if (self.navigationController.topViewController == self)
{
[self.navigationController popViewControllerAnimated:YES];
}
else
{
[self dismissViewControllerAnimated:YES completion:nil];
}
}
方法3
[objc] view plain copy 在CODE上查看代码片派生到我的代码片
- (void)backPreviousController
{
if ([self.navigationController.viewControllers.firstObject isEqual:self])
{
[self dismissViewControllerAnimated:YES completion:nil];
}
else
{
[self.navigationController popViewControllerAnimated:YES];
}
}
[html] view plain copy 在CODE上查看代码片派生到我的代码片
- (void)backPreviousController
{
if ([self.navigationController.viewControllers indexOfObject:self] == 0)
{
[self dismissViewControllerAnimated:YES completion:nil];
}
else
{
[self.navigationController popViewControllerAnimated:YES];
}
}