Swift Navigation Bar item not calling action

2019-06-21 15:10发布

I have an item on the navigation bar that calls an action. It stopped working. I have disconnected the item from the action and re-attached. Still no action. The action is attached in the storyboard. How do I debug or solve this issue?

2条回答
做个烂人
2楼-- · 2019-06-21 15:21

If you used the drag and drop functionality for creating the IBAction method, make sure it created the method for the button and not the "Bar Button Item."

The easiest way to check is by opening the Document Outline view in your Storyboard.

查看更多
爷、活的狠高调
3楼-- · 2019-06-21 15:30

Try something like this

If you have a parameter

 override func viewDidLoad() {
    super.viewDidLoad()

    self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "ButtonName", style: .done, target: self, action: #selector(YourViewController.yourAction(_:)))

}

Without parameter

 override func viewDidLoad() {
    super.viewDidLoad()

    self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "ButtonName", style: .done, target: self, action: #selector(YourViewController.yourAction))

}
查看更多
登录 后发表回答