iOS使用UINavigationController跳转页面怎么销毁当前页面

新手上路,请多包涵

iOS使用UINavigationController跳转页面怎么销毁当前页面。
就是我从a页面跳到b页面,我要销毁a页面。
还有就是我怎么进行页面的传值。

谢谢各位大佬。

阅读 10.5k
6 个回答

一个解决思路是,你不用销毁当前页面,你只需要返回的时候不返回到当前页面就可以了。
NavigationController是可以返回到rootController的。

navigationController?.popToRootViewController(animated: true)

另外,NavigationController还可以获取当前navigation stack中的所有View Controller。你就可以返回到你指定的页面。或者销毁stack中的任意view controller。

navigationController?.viewControllers

传值的话,你应该想问的是向前传值吧?有2种办法:

  1. 在 A Controlelr 中 创建 B Controller 时写属性操作进行传值
  2. 如果使用 storyboard, 则可以通过重写 A Controller 的 prepareForSegue:sender:方法进行传值
新手上路,请多包涵

试试a页面removeFromParentViewController,页面传值方法很多,要看你具体需求了

其实就是指定跳转位置,略过你栈中的一个或多个控制器。核心方法就是- (nullable UIViewController *)popViewControllerAnimated:(BOOL)animated;,只要在你的navigation.viewControllers中拿到你想要的控制器就可以了。以下代码可以略微参考一下,是自己写的navigation基类,实现点击返回的手动控制及位置指定

// .h
@property (nonatomic, assign) NSInteger targetIndex;
@property (nonatomic, copy) void(^backOperStandBy)(void(^handleBack)(BOOL back,NSInteger targetIndex),NSInteger operIndex);
// .m
- (void)sp_popViewController{
    
    if (self.backOperStandBy != nil) {
        __weak typeof(self) ws = self;
        self.backOperStandBy(^(BOOL back, NSInteger targetIndex) {
                if (back) {
                    ws.targetIndex = targetIndex;
                    [ws handlePopAction];
                }
        }, self.viewControllers.count-1);
    } else {
        [self handlePopAction];
    }
    
}

- (void)handlePopAction
{
    if (self.targetIndex == RootIndex) {
        [self popToRootViewControllerAnimated:YES];
        return;
    }
    
    if (self.targetIndex != 0 && self.targetIndex > 0) {
        UIViewController *target = self.viewControllers[self.targetIndex];
        self.targetIndex = 0;
        if (target) {
            [self popToViewController:target animated:YES];
        }else{
            [self popViewControllerAnimated:YES];
        }
        return;
    }
    // 普通情况
    [self popViewControllerAnimated:YES];
}

一般来说

// A页面下面跳到B页面
self.navigationController?.pushViewController(B_ViewController, animated: true)

可以改为

self.navigationController?.setViewControllers([B_ViewController], animated: true)
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题