在AppDelegate中设置barButtonItem

时间:2018-06-06 13:59:23

标签: ios swift xcode uibarbuttonitem appdelegate

如何在barButtonItem中设置AppDelegate?现在我有了这段代码:

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仍然没有出现。 如何使用我的操作backButton创建func goToMainVC

1 个答案:

答案 0 :(得分:0)

您没有将导航项设置为导航控制器,而是将导航项设置为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)

希望这有帮助