aLFaRSi
aLFaRSi

Reputation: 559

How to change Edit/Done button title in UINavigationBar

If Editing mode is enabled for the tableview it shows a button titled Edit, when pressing it it changes the title to done

i was wondering if there is a way to change the Done Button title to something else ?

i already changed the title for the done button.

the code i used is

self.navigationItem.rightBarButtonItem = self.editButtonItem;
self.editButtonItem.title = @"Change";

Now The Edit is Change

How to make Done to something else ?

Upvotes: 12

Views: 11055

Answers (3)

Benjohn
Benjohn

Reputation: 13887

Building on Nitin's answer I suggest a slightly different approach that uses the built in UIButtonBar system items.

This will give your UI the system look & feel. For example, the standard "Done" button to cease editing should have a specific bold look on iOS 8. Apple might change these stylings in future. By using the system defined buttons your app will pick up the current aesthetic automatically.

This approach also provides you with free string localisation. Apple have already translated the system button titles for you in to the dozens of languages that iOS supports.

Here's the code I've got:

-(IBAction) toggleEditing:(id)sender
{
  [self setEditing: !self.editing animated: YES];
}

-(void) setEditing:(BOOL)editing animated:(BOOL)animated
{
  [super setEditing: editing animated: animated];

  const UIBarButtonSystemItem systemItem = 
    editing ? 
    UIBarButtonSystemItemDone : 
    UIBarButtonSystemItemEdit;

  UIBarButtonItem *const newButton = 
    [[UIBarButtonItem alloc] 
      initWithBarButtonSystemItem: systemItem 
                           target: self 
                           action: @selector(toggleEditing:)];

  [self.navigationItem setRightBarButtonItems: @[newButton] animated: YES];
}

The example here is for the case where your UIViewController is hosted in a UINavigationController and so has a UINavigationItem instance. If you're not doing this, you'll need to update the bar item in an appropriate way.

In your viewDidLoad use the following call to configure the edit button ready for use:

[self setEditing: NO animated: NO];

Upvotes: 4

Nitin Gohel
Nitin Gohel

Reputation: 49730

You can change the title of the edit button like this:-

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    // Make sure you call super first
    [super setEditing:editing animated:animated];

    if (editing)
    {
        self.editButtonItem.title = NSLocalizedString(@"Cancel", @"Cancel");
    }
    else
    {
        self.editButtonItem.title = NSLocalizedString(@"Edit", @"Edit");
    }
}

its working like edit:-

enter image description here

TO

enter image description here

Upvotes: 10

MGM
MGM

Reputation: 2375

Here is the method to change it for Swift

override func setEditing (editing:Bool, animated:Bool)
{
    super.setEditing(editing,animated:animated)
    if (self.editing) {
        self.editButtonItem().title = "Editing"
    }
    else {
        self.editButtonItem().title = "Not Editing"
    }
}

Upvotes: 8

Related Questions