The selector does not work on my fast UIBarButtonItem

I am writing a to-do list application to learn how to quickly develop iOS. I had a settings button in the navigation bar, on the right, and whenever someone clicked the button above the table view, they could add a new item to their list. This causes the Cancel button to appear on the left side of the panel and the Add button to appear on the right side. However, when this is done, these buttons disappear, so I programmatically re-create the settings button with a call to the function that should call the session.

self.navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(named: "modify"), style: UIBarButtonItemStyle.Plain, target: nil, action: Selector("showSettings:")) 

This is the code that creates the button on the navigation bar (and it is actually created)

  func showSettings(sender: AnyObject?) { print("segueShouldBeDone!") performSegueWithIdentifier("showSettings", sender: sender) } 

And this is a function that should call segue (I added a print to see if it was even called, and it’s not).

I had the same problem elsewhere in my code, but I refused to fix it because it was not so important for me. But now it interferes with the application, so I would like to know how to fix it.

Any help would be great.

+5
source share
1 answer

The showSettings: selector showSettings: not called because you specified nil instead of self for the panel item target. Change the line to:

 self.navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(named: "modify"), style: UIBarButtonItemStyle.Plain, target: self, action: Selector("showSettings:")) 

Suppose the showSettings: method is in the same class that contains this line of code.

Read the documentation for this initializer, in particular information about the target parameter.

+10
source

Source: https://habr.com/ru/post/1232369/


All Articles