自定义C控制器返回按钮
1 UIBarButtonItem *backBarButton = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(back)]; 2 3 self.navigationItem.leftBarButtonItem = backBarButton;
监听back
- (void)back { UIViewController *popVC = self.navigationController.viewControllers[1]; NSLog(@"%@", self.navigationController.viewControllers); [self.navigationController popToViewController:popVC animated:YES]; }
具体要返回到第几个控制器 可以打印
self.navigationController.viewControllers 这是数组,可以看到里面每个控制器的顺序改变索引就可以任意跳转了 注意:如果没有自定义按钮 需要实现以下方法 避免可以左滑返回:
1 - (void)viewWillAppear:(BOOL)animated 2 { 3 4 self.navigationController.interactivePopGestureRecognizer.enabled = NO; 5 }
1 - (void)viewWillDisappear:(BOOL)animated 2 3 { 4 5 self.navigationController.interactivePopGestureRecognizer.enabled = YES; 6 7 }
时间: 2024-11-02 02:58:17