Search code examples
iosswiftrx-swiftrx-cocoa

How to toggle editButtonItem with rxswift?


I have the following setup

override func viewDidLoad() {

    super.viewDidLoad()

    navigationItem.leftBarButtonItem = editButtonItem

    navigationItem.leftBarButtonItem?.rx.tap.subscribe(onNext: { [weak self] _ in

        print("edit tapped")

        if let isEditing = self?.tableView.isEditing {

            self?.tableView.setEditing(!isEditing, animated: true)

        }

        }).disposed(by: disposeBag)
}

The problem is left bar button item is not toggled on tap. I.E text in the left bar button should change from Edit to Done.

Changing button title in onNext method does not work either.


Solution

  • You should call setEditing on self, not the table view. If self is a UITableViewController, this will also change the editing state of the table view.