views:

338

answers:

1

I have a favorite image to display on a UIBarButtonItem, on an toolbar.

How do you do to change it when this item is unselected/selected like this screenshost http://i50.tinypic.com/10gzehi.jpg ?

Thanks!

A: 

You can make two arrays with UIBarButtonItems: one with the first image and one with the second image. Like this:

// array with unselected
UIBarButtonItem *unselectedItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_unselected.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(doStuff:)];
self.itemsWithUnselected = [NSArray arrayWithObject:unselectedItem]; // declared as NSArray*
[unselectedItem release];

// array with selected
UIBarButtonItem *selectedItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_selected.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(doStuff:)];
self.itemsWithSelected = [NSArray arrayWithObject:selectedItem]; // declared as NSArray*
[selectedItem release];

and then switch between the two sets of toolbar items with:

toolbar.items = self.itemsWithSelected; // or self.itemsWithUnselected

If you have more than just the one button on your toolbar then just add the rest of the items to both arrays.

Jens Willy Johannsen