You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用UINavigationController,dismiss中间页后perform segue的转场需求

实现方案:关闭中间界面后跳转且保持导航栈正确

我完全get你的需求啦——流程是Blue(主界面)→ White(中间界面)→ 关闭White后自动跳转到Green,而且从Green返回时必须回到Blue,不能回到White。这种情况确实不能直接给White和Green连segue,不然导航栈会混乱,下面给你两种简洁靠谱的实现方式:

方法一:闭包回调(推荐,解耦且直观)

这是iOS中处理界面间回调最常用的方式,逻辑清晰还能避免循环引用。

1. 修改WhiteViewController,添加闭包属性

class WhiteViewController: UIViewController {
    // 定义一个闭包,用来通知主界面执行后续跳转
    var onDismissCompletion: (() -> Void)?

    // 假设这是你关闭White界面的按钮事件
    @IBAction func tapCloseWhite(_ sender: UIButton) {
        // 关闭界面的同时,在完成回调里触发闭包
        dismiss(animated: true) {
            self.onDismissCompletion?()
        }
    }
}

2. 在BlueViewController中绑定闭包逻辑

class BlueViewController: UIViewController {
    @IBAction func tapBlue(_ sender: Any) {
        performSegue(withIdentifier: "whiteSegue", sender: self)
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        guard segue.identifier == "whiteSegue" else { return }
        
        let whiteVC = segue.destination as! WhiteViewController
        // 绑定闭包:指定关闭White后要执行的跳转操作
        whiteVC.onDismissCompletion = { [weak self] in
            guard let self = self else { return }
            // 跳转到Green界面,这个segue是从Blue直接连到Green的
            self.performSegue(withIdentifier: "greenSegue", sender: self)
        }
    }
}

方法二:NotificationCenter通知(适合复杂跨界面场景)

如果你的项目中有更复杂的跨模块通信需求,用通知也能实现,但相比闭包会稍显繁琐:

1. WhiteViewController关闭时发送通知

class WhiteViewController: UIViewController {
    @IBAction func tapCloseWhite(_ sender: UIButton) {
        dismiss(animated: true) {
            // 发送自定义通知
            NotificationCenter.default.post(
                name: NSNotification.Name(rawValue: "WhiteViewControllerDismissed"),
                object: nil
            )
        }
    }
}

2. BlueViewController监听通知并执行跳转

class BlueViewController: UIViewController {
    @IBAction func tapBlue(_ sender: Any) {
        performSegue(withIdentifier: "whiteSegue", sender: self)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // 注册通知监听
        NotificationCenter.default.addObserver(
            self,
            selector: #selector(handleWhiteDismissed),
            name: NSNotification.Name(rawValue: "WhiteViewControllerDismissed"),
            object: nil
        )
    }

    deinit {
        // 务必移除通知,防止内存泄漏
        NotificationCenter.default.removeObserver(self)
    }

    @objc private func handleWhiteDismissed() {
        performSegue(withIdentifier: "greenSegue", sender: self)
    }
}

关键注意事项

  • 确保Storyboard中存在两个segue:Blue → White(标识符whiteSegue,建议用present方式),以及Blue → Green(标识符greenSegue,用push方式,这样Green会被加入导航栈,返回时自然回到Blue)。
  • 如果White是通过push进入的,那关闭时应该用navigationController?.popViewController(animated: true, completion: { ... }),并在completion里触发闭包/发送通知。

内容的提问来源于stack exchange,提问作者Andrew WC Brown

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 08:14:21