Swift Navigation Bar item not calling action

2019-06-21 15:06发布

问题:

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?

回答1:

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))

}


回答2:

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.