Home > database >  How to change TitleLabel - Bar Button Item?
How to change TitleLabel - Bar Button Item?

Time:08-10

I have Bar Button Item with Title "Edit" in UITableViewController.

    @IBOutlet weak var button: UIBarButtonItem!

@IBAction func pushEdit(_ sender: Any) {
    tableView.setEditing(!tableView.isEditing, animated: true)
}

I need to change TextLabel from "Edit" to "Done" when the user clicked on "Edit" button.

It seems that setTitle(_:for:) doesn't work.

What is the way to change Title?

CodePudding user response:

.title seems to be what you are looking for.

 @IBOutlet var saveButton: UIBarButtonItem!
    
 saveButton.title = "Saved"

CodePudding user response:

You can achieve this by toggling the editing mode and setting the title depending on whether your tableView is being edited

@IBAction func pushEdit(_ sender: Any) {
    tableView.isEditing.toggle()
    navigationItem.rightBarButtonItem?.title = tableView.isEditing ? "Done" : "Edit"
}
  • Related