简体   繁体   English

在AppDelegate中设置barButtonItem

[英]Set barButtonItem in AppDelegate

How can I set barButtonItem in AppDelegate ? 如何在AppDelegate设置barButtonItem Now I have this code: 现在我有这段代码:

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)

This line not helped me and barButtonItem still not appear. 这行没有帮助我, barButtonItem仍然没有出现。 How can I create backButton with my action func goToMainVC ? 如何创建backButton与我的动作func goToMainVC

You don't set the navigation item to navigation controller rather you set the navigation item to the navigation bar of viewController 您无需将导航项设置为导航控制器,而是将导航项设置为viewController的导航栏

so change your code as 所以将您的代码更改为

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)

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM