在 AppDelegate 中设置 barButtonItem
Set barButtonItem in AppDelegate
如何在 AppDelegate
中设置 barButtonItem
?现在我有了这个代码:
func presentDetailViewController(_ hallID: String) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let navVC = UINavigationController()
let newDetailVC = storyboard.instantiateViewController(withIdentifier: "newDetailVC") as! NewDetailTableViewController
newDetailVC.hallID = hallID
navVC.viewControllers = [newDetailVC]
let backItem = UIBarButtonItem(title: "Back", style: .plain, target: self, action: #selector(AppDelegate.goToMainVC))
navVC.navigationItem.setRightBarButton(backItem, animated: true)
window?.rootViewController = navVC
window?.makeKeyAndVisible()
}
@objc func goToMainVC() {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let navVC = UINavigationController()
let mainVC = storyboard.instantiateViewController(withIdentifier: "MainVC") as! PhotoStudiosViewController
navVC.viewControllers = [mainVC]
window?.rootViewController = navVC
window?.makeKeyAndVisible()
}
let backItem = UIBarButtonItem(title: "Назад", style: .plain, target: self, action: #selector(AppDelegate.goToMainVC))
navVC.navigationItem.setRightBarButton(backItem, animated: true)
这条线对我没有帮助,barButtonItem
仍然没有出现。
如何用我的操作 func goToMainVC
创建 backButton
?
您没有将导航项设置为导航控制器,而是将导航项设置为 viewController
的导航栏
所以将您的代码更改为
let newDetailVC = storyboard.instantiateViewController(withIdentifier: "newDetailVC") as! NewDetailTableViewController
let backItem = UIBarButtonItem(title: "Back", style: .plain, target: self, action: #selector(AppDelegate.goToMainVC))
//navVC.navigationItem.setRightBarButton(backItem, animated: true) this is the issue
newDetailVC.navigationItem.setRightBarButton(backItem, animated: true)
希望对您有所帮助
如何在 AppDelegate
中设置 barButtonItem
?现在我有了这个代码:
func presentDetailViewController(_ hallID: String) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let navVC = UINavigationController()
let newDetailVC = storyboard.instantiateViewController(withIdentifier: "newDetailVC") as! NewDetailTableViewController
newDetailVC.hallID = hallID
navVC.viewControllers = [newDetailVC]
let backItem = UIBarButtonItem(title: "Back", style: .plain, target: self, action: #selector(AppDelegate.goToMainVC))
navVC.navigationItem.setRightBarButton(backItem, animated: true)
window?.rootViewController = navVC
window?.makeKeyAndVisible()
}
@objc func goToMainVC() {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let navVC = UINavigationController()
let mainVC = storyboard.instantiateViewController(withIdentifier: "MainVC") as! PhotoStudiosViewController
navVC.viewControllers = [mainVC]
window?.rootViewController = navVC
window?.makeKeyAndVisible()
}
let backItem = UIBarButtonItem(title: "Назад", style: .plain, target: self, action: #selector(AppDelegate.goToMainVC))
navVC.navigationItem.setRightBarButton(backItem, animated: true)
这条线对我没有帮助,barButtonItem
仍然没有出现。
如何用我的操作 func goToMainVC
创建 backButton
?
您没有将导航项设置为导航控制器,而是将导航项设置为 viewController
的导航栏所以将您的代码更改为
let newDetailVC = storyboard.instantiateViewController(withIdentifier: "newDetailVC") as! NewDetailTableViewController
let backItem = UIBarButtonItem(title: "Back", style: .plain, target: self, action: #selector(AppDelegate.goToMainVC))
//navVC.navigationItem.setRightBarButton(backItem, animated: true) this is the issue
newDetailVC.navigationItem.setRightBarButton(backItem, animated: true)
希望对您有所帮助