How to set target and action for UIBarButtonItem at runtime

后端 未结 11 1970
小蘑菇
小蘑菇 2020-12-15 02:44

Tried this but only works for UIButton:

[btn setTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
11条回答
  •  暖寄归人
    2020-12-15 02:46

    Set target and action of your UIBarButtonItem

    Swift 5 & 4

    button.target = self
    button.action = #selector(action)
    
    @objc func action (sender:UIButton) {
        print("action")
    }
    

提交回复
热议问题