UIBarButtonItem selector not working

后端 未结 2 1416
广开言路
广开言路 2020-12-21 13:56

I have a MainViewController embed in a Navigation Controller, as shown below:

And in MainViewController.swift, I added two UIBarButtonItem(left and right) progr

2条回答
  •  误落风尘
    2020-12-21 14:13

    try with inside scope once

    override func viewDidLoad() {
        super.viewDidLoad()
    
        let rightButton = UIBarButtonItem(title: "Right", style: .plain, target: self, action: #selector(self.onRightLeftClick(_ :)))
        rightButton.tag = 1
        let leftButton = UIBarButtonItem(title: "Left", style: .plain, target: self, action: #selector(self.onRightLeftClick(_ :)))
        rightButton.tag = 2
        self.navigationItem.rightBarButtonItem = rightButton
        self.navigationItem.leftBarButtonItem = leftButton
    }
    

    handle the action as

    func onRightLeftClick(_ sender: UIBarButtonItem){
        if sender.tag == 1{
            // rightButton action
        }else{
            // leftButton action
        }
    }
    

提交回复
热议问题