如何在swift 3中以编程方式为barButtonItem设置操作?

时间:2016-09-29 11:07:50

标签: ios swift selector swift3 uibarbuttonitem

这是我之前使用过的,

var barButtonItem = UIBarButtonItem(image: backImgs, style: UIBarButtonItemStyle.plain, target: self, action: Selector("menuButtonTapped:"))

但是Swift 3有一些语法更改。提前感谢。

12 个答案:

答案 0 :(得分:94)

前: -

navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Add", style: .plain, target: self, action: #selector(addTapped))

答案 1 :(得分:49)

总结Swift 3中最常用的方法,用于向barButton添加动作。

  1. Barbutton的文字

    navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Add", style: .plain, target: self, action: #selector(addTapped))
    
  2. 带有您自己形象的BarButton

    navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(named:"add"), style: .plain, target: self, action: #selector(addTapped))
    
  3. 带系统图像的BarButton

    navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addTapped))
    

答案 2 :(得分:28)

如果有人使用customView

barButtonItem.customView?.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(onBarButtonItemClicked)))

答案 3 :(得分:14)

您只需要在Swift 3中更改call.enqueue(new Callback<UserStatistic>() { 语法,您需要在函数调用中指定方法的第一个参数名称,因此请更改您的选择器。

selector

你的方法应该是这样的。

#selector(menuButtonTapped(sender:))

答案 4 :(得分:12)

适用于iOS 10.1的Swift 3上的一行代码:

navigationController?.navigationBar.topItem?.rightBarButtonItem = UIBarButtonItem(title: "Add", style: .plain, target: self, action: nil)

答案 5 :(得分:8)

let barButtonItem = UIBarButtonItem(image: UIImage(named: "backImgs"),
                                            style: .plain,
                                            target: self,
                                            action: #selector(menuButtonTapped))

// Adding button to navigation bar (rightBarButtonItem or leftBarButtonItem)
self.navigationItem.rightBarButtonItem = barButtonItem

 // Private action
@objc fileprivate func menuButtonTapped() { // body method here }

答案 6 :(得分:6)

为barbutton项创建扩展名。

 extension UINavigationItem {
    func addSettingButtonOnRight(){
       let button = UIButton(type: .Custom)
       button.setTitle("setting", forState: .Normal)
       button.titleLabel?.font = UIFont.systemFontOfSize(15.0)
       button.layer.cornerRadius = 5
       button.backgroundColor = UIColor.grayColor()
       button.frame = CGRect(x: 0, y: 0, width: 100, height: 25)
       button.addTarget(self, action: #selector(gotSettingPage), forControlEvents: UIControlEvents.TouchUpInside)
       let barButton = UIBarButtonItem(customView: button)

       self.rightBarButtonItem = barButton
   }

   func gotSettingPage(){

   }
 }

从viewDidLoad()

调用它
 self.navigationItem.addSettingButtonOnRight()

答案 7 :(得分:5)

对于Swift 4,在viewDidLoad中添加:

navigationItem.rightBarButtonItem = UIBarButtonItem(
    barButtonSystemItem: UIBarButtonSystemItem.add, 
    target: self, 
    action: #selector(addTransaction)
)

答案 8 :(得分:4)

Swift 3 中,您可以像这样添加UIBarButtonItem,

let addButton = UIBarButtonItem(image:UIImage(named:"your_icon_name"), style:.plain, target:self, action:#selector(YourControllerName.buttonAction(_:)))
addButton.tintColor = UIColor.white
self.navigationItem.rightBarButtonItem = addButton

并处理按钮动作,

func buttonAction(_ sender: UIBarButtonItem) {

}

希望它有所帮助。

答案 9 :(得分:1)

制作UIBarButtonItem:

let rightButton: UIBarButtonItem = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.done, target: self, action: #selector(LocationViewController.doneButtonClicked(_:)))

添加到NavigationItem:

self.navigationItem.rightBarButtonItem = rightButton

相关功能:

func doneButtonClicked(_ button:UIBarButtonItem!){    
    print("Done clicked")    
}

答案 10 :(得分:1)

如果有人正在寻找对现有按钮进行操作的方法(快捷键5):

if let leftBarButtonItem = navigationItem.leftBarButtonItem {
    leftBarButtonItem.target = self
    leftBarButtonItem.action = #selector(onBack(_:))
}


@IBAction func onBack(_ sender: AnyObject) {
    _ = navigationController?.popViewController(animated: true)
}

答案 11 :(得分:0)

在Swift 5中

//For righter button item
let rightBtn = UIBarButtonItem(image: UIImage(named: "rightmenu"), style: .plain, target: self, action: #selector(onClickMethod))//Change your function name and image name here
self.navigationItem.rightBarButtonItem = rightBtn
//self.navigationItem.rightBarButtonItem = [rightBtn, anotherBtn] //If you want to add more buttons add like this

//For left bar button item
let leftBtn = UIBarButtonItem(image: UIImage(named: "rightmenu"), style: .plain, target: self, action: #selector(onClickMethod)) //Change your function name and image name here
self.navigationItem.leftItemsSupplementBackButton = true
self.navigationItem.leftBarButtonItem = leftBtn
//self.navigationItem.leftBarButtonItems = [leftBtn, anotherBtn] //If you want to add more buttons add like this

//This is your function name
@objc func onClickMethod() {
    print("Left bar button item")
}