Fernando Augusto Fernando Augusto - 6 days ago 6
Swift Question

My add button in my iOS 10 app is not performing an action

I started to learn Swift 3 and Xcode a week ago.

At this moment I have an small app with a navigation bar and an add button on the right, I want it to perform something but I can't figure it out...

I have this:

func criarPessoa() {
_ = pessoas.append("Ola OLA")
}

let adicionarButao = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(criarPessoa()(sender:)))


What am I doing wrong?

Answer
let adicionarButao = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(criarPessoa()(sender:)))

should be

let adicionarButao = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(NameOfTheVCTheFunctionIsIn.criarPessoa()))

You also need to add the BarButton like so :

self.navigationItem.setLeftBarButtonItems([adicionarButao], animated: false)

or:

self.navigationItem.setRightBarButtonItems([adicionarButao], animated: false)
Comments