如何将barButtonItem添加到ToolBar

时间:2016-03-07 04:42:17

标签: swift toolbar uibarbuttonitem

如何转换此代码以使按钮显示在工具栏中? :

navigationItem.rightBarButtonItem = editButtonItem()

2 个答案:

答案 0 :(得分:0)

试试这个,

    let barButtonItem = UIBarButtonItem(title: "Edit", style: .Plain, target: self, action: "onEditButtonTapped:")
    self.navigationItem.rightBarButtonItem = barButtonItem

然后编写动作函数,就像这样,

    func onEditButtonTapped(sender:UIBarButtonItem) {
        ...
    } 

答案 1 :(得分:0)

试试这个:

    self.navigationItem.setRightBarButtonItem(UIBarButtonItem(barButtonSystemItem: UIBarButtonItemStyle.Plain, target: self, action: "btnClicked:"), animated: true)


  //adding function to bar button

    func btnClicked(sender: UIBarButtonItem)
     {
        //your code here
     }