In my case, I wanted to toggle between the Edit|Done.
However, I couldn’t use the leftBarButtonItem because I already had another UIBarButtonItem
.
What I did is the following:
1- Create @IBOutlet weak var edit: UIBarButtonItem!
2- Then a variable to hold the state: var isEditingMode = false
3- Now in the viewDidLoad
:
override func viewDidLoad() {
…
self.edit.action = #selector(self.toogleEditor(_:))
self.edit.title = "Edit"
self.setEditing(isEditingMode, animated: true)
…
}
I initialize the edit.action Selector to my custom function toogleEditor
. I want to be able to change the title whenever an action occur.
4- Create an IBAction:
@IBAction func toogleEditor(sender: AnyObject) {
if isEditingMode
{
isEditingMode = false
self.edit.title = "Edit"
}
else
{
isEditingMode = true
self.edit.title = "Done"
}
self.setEditing(isEditingMode, animated: true)
}
This function is triggered each time the user click the UIBarItemButton
.
The only thing to do is use the setEditing(…)
to change the behaviour of the UITableViewController
.