我的应用程序中有两个 View Controller 。 Root View Controller 有一些组件,其中之一是按钮。该按钮提供使用当前功能打开另一个 View Controller 。点击按钮时打开的第二个 View Controller (SelectTimeViewController)已成功打开。我正在尝试设置导航标题和项目,但看不到它们。
我没有使用 Storyboard,因此 Root View Controller 是从 AppDelegate 设置的。
let viewController = ViewController()
window = UIWindow(frame: UIScreen.main.bounds)
window?.rootViewController = UINavigationController(rootViewController: viewController)
window?.makeKeyAndVisible()
点击按钮时,将调用“openVC”函数。
@IBAction func openVC(_ sender: Any) {
self.navigationController?.present(SelectTimeViewController(), animated: true, completion: nil)
}
我试图在 SelectTimeViewController 的 viewDidLoad 函数中设置 title 和 rightBarButtonItem,但我看不到它们。
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(named: "close"), style: .plain, target: self, action: #selector(closeIt))
self.navigationItem.title = "Select Time"
}
除此之外,当更改“openVC”功能时,我可以看到标题和右栏按钮项目,如下所示。
@IBAction func openVC(_ sender: Any) {
self.navigationController?.pushViewController(vc, animated: true)
}
最佳答案
您必须推送 SelectTimeViewController 而不是当前
或者,如果您确实想呈现它,您应该呈现另一个 UINavigationController,其 rootViewController 为 SelectTimeViewController()
关于swift - 如何使用当前模态打开另一个 View Controller 后设置导航项的标题 - Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58396984/