一周前,我开始学习Swift 3和Xcode。

目前,我有一个带有导航栏和右侧添加按钮的小应用程序,我希望它执行某些操作,但我无法弄清楚...

我有这个:

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

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


我究竟做错了什么?

最佳答案

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


应该

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


您还需要像这样添加BarButton:

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


要么:

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

10-08 08:06