Home > database >  I want to make a UIBarButtonItem works with setEditing
I want to make a UIBarButtonItem works with setEditing

Time:11-07

I am having trouble dealing with UIBarButtonItem With setEditing

    let add = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addTapped))
let play = UIBarButtonItem(title: "Play", style: .plain, target: self, action: #selector(playTapped))

navigationItem.rightBarButtonItems = [add, play]

When writing constants (add , play ) outside viewDidLoad

I can call a constant name in a function setEditing

override func setEditing(_ editing: Bool, animated: Bool) {
    super.setEditing(editing, animated: animated)
    
    if isEditing{
        
    }else{

    }

But it is writing anything, even if it is printed, it does not work

And when writing it inside viewDidLoad

Everything works, but I can't call it in a function setEditing

I want to hide the play button and show it in the setEditing function

CodePudding user response:

I was thinking at something like :

class ViewController: UIVewController {
    var add : UIBarButtonItem?
    var play : UIBarButtonItem?

override func viewDidLoad() {
    super.viewDidLoad()

    add = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addTapped))
    play = UIBarButtonItem(barButtonSystemItem: .play, target: self, action: #selector(playTapped))
    navigationItem.rightBarButtonItems = [add!]
    
}

override func setEditing(_ editing: Bool, animated: Bool) {
super.setEditing(editing, animated: animated)

if isEditing{

    navigationItem.rightBarButtonItems = [add!]

    
}else{

    navigationItem.rightBarButtonItems = [add!, play!]

}
  • Related