How to programmatically set action for barButtonItem in swift 3?

后端 未结 12 1438
鱼传尺愫
鱼传尺愫 2020-12-23 16:17

Here is what I used previously,

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


        
相关标签:
12条回答
  • 2020-12-23 16:45

    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")    
    }
    
    0 讨论(0)
  • 2020-12-23 16:47
    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 }
    
    0 讨论(0)
  • 2020-12-23 16:52

    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))
      
    0 讨论(0)
  • 2020-12-23 16:52

    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)
    }
    
    0 讨论(0)
  • 2020-12-23 16:53

    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) {
    
    }
    
    0 讨论(0)
  • 2020-12-23 16:58

    If anyone is using customView:

    barButtonItem.customView?.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(onBarButtonItemClicked)))
    
    0 讨论(0)
提交回复
热议问题