使导航栏显示所需的标题

Make the navigation bar show the required title

我有 3 个视图控制器:VC1 -> VC3 -> VC2。从 VC3 转换到 VC2 我用下一个代码:

let storyboard = UIStoryboard(name: "VCs", bundle: nil)
let myVC2 = storyboard.instantiateViewController(withIdentifier: "VC2") as? VC2
myVC2?.name = "Test"
self.navigationController?.pushViewController(myVC2!, animated: true)

在 VC2 的 viewDidLoad() 中,我从 viewControllers 堆栈中删除了 VC3,只留下 VC1:

var navigationVCs = self.navigationController?.viewControllers
navigationVCs!.remove(at: 1)
self.navigationController?.viewControllers = navigationVCs!

一切正常。但是我有一个视觉 "Issue"。当视图控制器从VC4移动到VC3,VC3视图开始加载时,VC3的NavigationBar后退按钮首先显示VC4的标题,然后才切换到VC1的标题。我该如何解决这个问题并强制 VC2 显示 VC1 的后退按钮标题?谢谢

在VC3中可以将VC1的后退按钮标题传给VC2:

if let navigationController = UIApplication.shared.keyWindow?.rootViewController as? UINavigationController {
            if let previousVC = navigationController.viewControllers.first {
                let backItem = UIBarButtonItem()
                let title = previousVC.navigationItem.backBarButtonItem?.title
                backItem.title = title
                navigationItem.backBarButtonItem = backItem // This will show in the next view controller being pushed
            }
        }

在你打电话之前

self.navigationController?.pushViewController(myVC2!, animated: true)