iOS-如何在Swift 3中以编程方式为uibarButtonItem设置操作?

这是我以前使用的

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

但是Swift 3有一些语法更改。预先感谢。

Bhanupriya asked 2020-07-11T13:25:30Z
12个解决方案
101 votes

例如:-

navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Add", style: .plain, target: self, action: #selector(addTapped))
Tony Vincent answered 2020-07-11T13:25:39Z
54 votes

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

  1. 带有文本的按钮

    navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addTapped))
    
  2. 带有您自己的图像的BarButton

    navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addTapped))
    
  3. 具有系统映像的BarButton

    navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addTapped))
    
flame3 answered 2020-07-11T13:26:12Z
31 votes

如果有人在使用customView

barButtonItem.customView?.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(onBarButtonItemClicked)))
Onuray Sahin answered 2020-07-11T13:26:31Z
14 votes

从Swift 3开始,您只需要更改selector语法,您就需要在函数调用中指定method的第一个参数名称,因此需要像这样更改选择器。

#selector(menuButtonTapped(sender:))

您的方法应该是这样的。

func menuButtonTapped(sender: UIBarButtonItem) {

}
Nirav D answered 2020-07-11T13:26:56Z
12 votes

Swift 3 for iOS 10.1上的一行代码:

navigationController?.navigationBar.topItem?.rightBarButtonItem = UIBarButtonItem(title: "Add", style: .plain, target: self, action: nil)
Marco Aurelio Americo answered 2020-07-11T13:27:17Z
8 votes
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 }
Mihail Salari answered 2020-07-11T13:27:32Z
6 votes

为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()
Mr. Tann answered 2020-07-11T13:27:57Z
6 votes

对于Swift 4添加viewDidLoad

navigationItem.rightBarButtonItem = UIBarButtonItem(
    barButtonSystemItem: UIBarButtonSystemItem.add, 
    target: self, 
    action: #selector(addTransaction)
)
pankaj nigam answered 2020-07-11T13:28:17Z
5 votes

在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) {

}

希望能帮助到你。

Riajur Rahman answered 2020-07-11T13:28:47Z
4 votes

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

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


@IBAction func onBack(_ sender: AnyObject) {
    _ = navigationController?.popViewController(animated: true)
}
ergunkocak answered 2020-07-11T13:29:07Z
1 votes

制作一个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")    
}
A.G answered 2020-07-11T13:29:35Z
0 votes

在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")
}
iOS answered 2020-07-11T13:29:56Z
translate from https://stackoverflow.com:/questions/39768600/how-to-programmatically-set-action-for-barbuttonitem-in-swift-3