views:

48

answers:

1

I've been breaking my head for the past two days searching and trying some of my own solutions. I placed a UIBarButtonItem through IB with an image in the top bar to act as a mute/unmute button . Everything works except the image doesn't change. I used the following code and it compiles but no change

if( mute == YES ) {
    UIImage *unmuteImage = [UIImage imageNamed:@"audio-on.png"];
    [self.muteButton setImage:unmuteImage];
    [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"muteKey"];
}
else {
    UIImage *muteImage = [UIImage imageNamed:@"audio-off.png"];
    [self.muteButton setImage:muteImage];
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"muteKey"];
}
A: 

I finally figured it out...only took a couple of days but I've been too busy to post up a solution. We'll I finally got time and am happy to post my solution. I had a hunch that this would'nt work unless it was done 100% programmatically, and I was right. Here's the final solution to my problem:

if(mute == YES)
{
    UIImage *image = [UIImage imageNamed:@"audio-off.png"];
    UIButton *myMuteButton = [UIButton buttonWithType:UIButtonTypeCustom];
    myMuteButton.bounds = CGRectMake( 0, 0, image.size.width, image.size.height );    
    [myMuteButton setImage:image forState:UIControlStateNormal];
    [myMuteButton addTarget:self action:@selector(mute) forControlEvents:UIControlEventTouchUpInside];    
    UIBarButtonItem *myMuteBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:myMuteButton];   
    navBar.leftBarButtonItem = myMuteBarButtonItem;
    [myMuteBarButtonItem release];
}
else
{
    UIImage *image = [UIImage imageNamed:@"audio-on.png"];
    UIButton *myUnmuteButton = [UIButton buttonWithType:UIButtonTypeCustom];
    myUnmuteButton.bounds = CGRectMake( 0, 0, image.size.width, image.size.height );    
    [myUnmuteButton setImage:image forState:UIControlStateNormal];
    [myUnmuteButton addTarget:self action:@selector(mute) forControlEvents:UIControlEventTouchUpInside];    
    UIBarButtonItem *myUnmuteBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:myUnmuteButton];
    navBar.leftBarButtonItem = myUnmuteBarButtonItem;
    [myUnmuteBarButtonItem release];
}

the good news is I finally finished my app and submitted it to the app store. Hopefully everything will go smooth and am looking forward to it!

Joseph Stein