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

导航控制器推送新视图控制器异常:跳转后仍显示原控制器

问题分析与解决

核心问题出在详情视图控制器的继承关系上:你写的FlagViewController继承自ViewController(你的主视图控制器),而不是系统的UIViewController。这就导致每次实例化FlagViewController时,它会继承主VC的所有属性和逻辑——包括你添加的UITableView子视图,所以跳转后看起来和原页面几乎一样,只是背景色变成了systemMint,看起来像是停留在原页面。

修复步骤

把FlagViewController的父类改为UIViewController即可,修正后的代码如下:

import UIKit

class FlagViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = .systemMint
    }
}

额外优化建议

在tableView(_:didSelectRowAt:)方法里添加取消cell选中状态的代码,提升交互体验:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    tableView.deselectRow(at: indexPath, animated: true) // 取消选中
    let vc = FlagViewController()
    navigationController?.pushViewController(vc, animated: true)
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.07 17:13:14