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

退出第二个ViewController时触发UINavigationBar解码错误,求排查

解决退出ViewController2时的UINavigationBar解码错误

退出第二个ViewController时反复出现以下错误提示:

"UINavigationBar decoded as unlocked for UINavigationController, or navigationBar delegate set up incorrectly."

问题排查与解决方案

这个错误和代码里的变量赋值逻辑无关,主要出在Storyboard的导航栏配置或segue设置上,按以下步骤排查:

  • 检查导航控制器配置:打开Storyboard,确认ViewController1被UINavigationController包裹。选中导航控制器,在右侧Attributes Inspector中找到Navigation Bar选项,确保其值为Default或Locked,不要设置为Unlocked。
  • 修正Unwind Segue设置:从ViewController2的返回按钮拖到Exit图标时,务必选择正确的unwindSegue方法,避免重复创建segue或关联错误方法。不要手动给UINavigationBar设置delegate,系统会自动管理,手动设置容易引发冲突。
  • 确认ViewController2的弹出方式:如果VC2是通过push segue从导航栈进入的,退出时优先用popViewController(animated:),而非unwind segue(除非需要回退到更早的VC);如果是modal弹出,unwind设置没问题的话,重点检查导航栏配置。
  • 清理缓存重新编译:若以上都没问题,尝试清理项目缓存(快捷键Command+Shift+K),再重新编译运行。

对应中文注释代码

ViewController1:

class ViewController1: UIViewController {
    // 接收VC2传递的值
    var getVal: String = "val"

    override func viewDidLoad() {
        super.viewDidLoad()
        label1.text = getVal
    }

    @IBOutlet weak var label1: UILabel!

    // 退回到VC1的unwind方法
    @IBAction func unwindSegue(for unwindSegue: UIStoryboardSegue, sender: Any?) {
        print(getVal)
    }
}

ViewController2:

class ViewController2: UIViewController {
    // 要传递给VC1的变量
    var cheese: String = "American"

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    // 修改变量值的按钮事件
    @IBAction func changeVar(_ sender: UIButton) {
        cheese = "Swiss"
        print(cheese)
    }

    // 准备传递数据到目标VC
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let vc = segue.destination as! ViewController1
        vc.getVal = cheese
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.15 22:05:21