how to programmatically set action for barButtonItem in swift 3?

我怕爱的太早我们不能终老 提交于 2019-12-18 10:26:41

问题


Here is what I used previously,

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

But there is some syntax changes for Swift 3.


回答1:


ex:-

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



回答2:


Summarize the mostly used method in Swift 3 for adding action to a barButton.

  1. Barbutton with text

    navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Add", style: .plain, target: self, action: #selector(addTapped))
    
  2. BarButton with your own image

    navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(named:"add"), style: .plain, target: self, action: #selector(addTapped))
    
  3. BarButton with system image

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



回答3:


If anyone is using customView:

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



回答4:


You just need to change your selector syntax as of from Swift 3 you need to specify the first parameter name of method in your function call so change your selector like this.

#selector(menuButtonTapped(sender:))

And your method should be like this.

func menuButtonTapped(sender: UIBarButtonItem) {

}



回答5:


One line of code on Swift 3 for iOS 10.1:

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



回答6:


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 }



回答7:


create an extension for barbutton item.

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

   }
 }

And call it from viewDidLoad()

 self.navigationItem.addSettingButtonOnRight()



回答8:


for Swift 4 add in viewDidLoad:

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



回答9:


In Swift 3, you can add UIBarButtonItem like that,

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

And handle button action like that,

func buttonAction(_ sender: UIBarButtonItem) {

}

Hope it helps.




回答10:


If someone is looking for a way to set the action on an existing button (Swift 5):

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


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



回答11:


Make a UIBarButtonItem:

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

Add to NavigationItem:

self.navigationItem.rightBarButtonItem = rightButton

Associated function:

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



回答12:


In 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")
}


来源:https://stackoverflow.com/questions/39768600/how-to-programmatically-set-action-for-barbuttonitem-in-swift-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!