iOS 关于NavigationController返回的一些笔记

1、理解NavigationController返回机制

一般NavigationController下的子view只有一层,子view返回上一级则可以直接用

[self.navigationController popViewControllerAnimated:YES];

如果NavigationController下有好几层子view,当前子view返回上一层,则可以用

[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:([self.navigationController.viewControllers count] -2)] animated:YES];

-3为上上一层,依此类推。push和pop的方法类似。

2、手势右滑返回上一层

控制器添加代理UIGestureRecognizerDelegate

self.navigationController.interactivePopGestureRecognizer.delegate = self;

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
    
    //判断是否是导航条的第一个子视图控制器
    if (self.navigationController && [self.navigationController.viewControllers count] >= 2) {
        return YES;
    }else{
        return NO;
    }
}

猜你喜欢

转载自www.cnblogs.com/sheer-code/p/10856923.html