backBarbuttonitem什么都没做?

时间:2016-08-23 23:11:33

标签: ios swift uinavigationcontroller

我已经以编程方式设置了导航

在视图控制器中我设置了一个后退按钮,只根据文档更改标题。 但是,单击子控制器中的后退按钮不会执行任何操作。

我错过了什么?

> in a viewController

    let backItem = UIBarButtonItem()
    backItem.title = ""
    navigationItem.backBarButtonItem = backItem
    self.navigationController?.pushViewController(secondViewController, animated: true)

1 个答案:

答案 0 :(得分:1)

您必须向按钮添加目标和操作

let backItem = UIBarButtonItem()
backItem.title = ""
backItem.target = self
backItem.action = #selector(back)
navigationItem.backBarButtonItem = backItem
self.navigationController?.pushViewController(secondViewController, animated: true)

然后实现back()函数。

func back() {
    // If your view controller was presented by a navigation controller
    self.navigationController?.popViewControllerAnimated(true)

    // If your view controller was presented modally
    self.presentingViewController?.dismiss(animated: true, completion: nil)
}