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

Swift UIKit中Sheet内打开新视图无响应问题求助

问题原因与解决方案

核心问题

你的代码点击标签后无跳转反应的根源是:myConfigViewController是通过present方式弹出的Sheet,它本身并未嵌入UINavigationController,因此self.navigationController的值为nil,导致pushViewController语句完全无法执行。

修复方案

修改openNewViewLabelTapped方法,在关闭Sheet后,获取弹出当前Sheet的原视图控制器的导航控制器,通过它来完成新页面的跳转:

@objc func openNewViewLabelTapped() {
    print("Open the new View")
    self.dismiss(animated: true) {
        // 获取弹出当前Sheet的原视图控制器
        guard let presentingVC = self.presentingViewController else { return }
        
        // 优先通过原VC的导航控制器push新页面
        if let navController = presentingVC.navigationController {
            guard let destViewController = self.storyboard?.instantiateViewController(withIdentifier: "isNewView") as? isNewViewController else {
                print("无法实例化isNewViewController")
                return
            }
            navController.pushViewController(destViewController, animated: true)
        } else {
            // 若原VC不在导航栈中,直接present新页面
            guard let destViewController = self.storyboard?.instantiateViewController(withIdentifier: "isNewView") as? isNewViewController else {
                print("无法实例化isNewViewController")
                return
            }
            presentingVC.present(destViewController, animated: true)
        }
    }
}

额外注意事项

  1. 确保弹出Sheet的原视图控制器(即调用present(sheet, animated: true)的VC)处于UINavigationController的管理之下,否则presentingVC.navigationController会为nil,此时可选择用present方式打开新页面。
  2. 替换代码中的强制解包(!)为安全校验(guard/if let),避免因storyboard ID错误或类型转换失败导致崩溃。

内容的提问来源于stack exchange,提问作者web216

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.12 20:46:18