UIBarButtonItem possibleTitles property

| | Comments (0)

The possibleTitles property of UIBarButtonItem is pretty useful if you need to change the title and also animate the changes. I was trying to animate both the back button and the title of the right barbutton at the same time, but the back button wouldn't disappear properly (i.e. not animated) until I set the possibleTitles of the right barbutton.

First create the button. Do this in viewDidLoad.

self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Edit" style:UIBarButtonItemStyleBordered target:self action:@selector(toggleEditing)] autorelease];
self.navigationItem.rightBarButtonItem.possibleTitles = [NSSet setWithObjects:@"Edit", @"Done", nil];

To change the title just set the title property. Very easy. Both changes will be animated.

- (void)toggleEditing {
	if (!self.editing) {
		self.navigationItem.rightBarButtonItem.title = @"Done";
		[self.navigationItem setHidesBackButton:YES animated:YES];
	} else {
		self.navigationItem.rightBarButtonItem.title = @"Edit";
		[self.navigationItem setHidesBackButton:NO animated:YES];
	}
}

This works in OS 3.0.

Leave a comment