我有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!
一切正常。但是我有一个视觉上的“问题”。当视图控制器从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)
关于ios - 使导航栏显示所需标题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57592223/