Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Having trouble changing a UIBarButtonItem's image

I'm trying various ways of changing a UIBarButtonItem's image once it has been pressed, without any luck.

// bookmarkButton is a property linked up in IB
-(IBAction)bookmarkButtonTapped:(id)sender
{
NSLog(@"this action triggers");
// attempt 1
UIBarButtonItem* aBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"bookmarkdelete.png"] style:UIBarButtonItemStylePlain target:self action:@selector(bookmarkButtonTapped:)];
bookmarkButton = aBarButtonItem;
[aBarButtonItem release];

// attempt 2
bookmarkButton.image = [UIImage imageNamed:@"bookmarkdelete.png"];
}

Is there another way to do this?

like image 545
cannyboy Avatar asked Nov 22 '10 14:11

cannyboy


1 Answers

The toolbar contains an array - items - as a property. So after setting up the toolbar as an IBOutlet property, I had to insert a new button into that array.. like this:

NSMutableArray *items = [[NSMutableArray alloc] initWithArray:self.toolBar.items];
UIBarButtonItem *newButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"newButton.png"]  style:UIBarButtonItemStylePlain target:self action:@selector(buttonTapped:)];
[items replaceObjectAtIndex:0 withObject:newButton];
self.toolBar.items = items;
[newButton release];
[items release];
like image 93
cannyboy Avatar answered Oct 21 '22 12:10

cannyboy