Umair A.
Umair A.

Reputation: 6873

UIButton setImage not working?

I have following code and when I call this method, it doesn't change the image of the button although there are images in the resources.

-(void)changeButton:(NSString*)button withURL:(NSString*)url {
    timetableURL = url;

    UIImage *btnImage;

    if ([button isEqual:@"Browser"]) {
        btnImage = [UIImage imageNamed:@"browser-button.png"];
        [safariBtn addTarget:self action:@selector(onSafariButtonPress:) forControlEvents:UIControlEventTouchUpInside];
    }
    else if ([button isEqual:@"Timetable"]) {
        btnImage = [UIImage imageNamed:@"browser-timetable.png"];  
        [safariBtn addTarget:self action:@selector(onTimetableButtonPress:) forControlEvents:UIControlEventTouchUpInside];
    }

    [safariBtn setImage:btnImage forState:UIControlStateNormal];
    [btnImage release];
}

UPDATED

FYI: It's a button upside UIToolbar.

Upvotes: 3

Views: 4593

Answers (5)

jrturton
jrturton

Reputation: 119242

Change

[button isEqual:@"Browser"]

To

[button isEqualToString:@"Browser"]

isEqual is testing for object equality, not string equality.

EDIT: OK, you say your button is inside a toolbar. In this case I assume you have added it as a customview to a UIBarbuttonItem? You need to go through the bar button item and set the image property on the custom view of the bar button item, I think (though I'm not sure) once you've added a button as a custom view, the bar button item makes it's own copy so you are not talking to it any more via safariBtn.

You can access the toolbar items via

for (UIBarButtonItem *barItem in self.toolbar.items)

Assuming this code is in your viewController. Your safariBtn will then be accessible via barItem.customView

Upvotes: 0

fanatic
fanatic

Reputation: 53

may be this will help you : set the titles of button where you created it and then control if it equals the title.

[safariBtn setTitle:@"Safari" forState:UIControlStateNormal];

then control

-(void)changeButton:(NSString*)button withURL:(NSString*)url {
timetableURL = url;

UIImage *btnImage;

if ([button titleForState:UIControlStateNormal] isEqualToString:@"Safari"]) {
    btnImage = [UIImage imageNamed:@"browser-button.png"];
    [safariBtn addTarget:self action:@selector(onSafariButtonPress:) forControlEvents:UIControlEventTouchUpInside];
}
else if ([button titleForState:UIControlStateNormal] isEqualToString:@"Timetable"]) {
    btnImage = [UIImage imageNamed:@"browser-timetable.png"];  
    [safariBtn addTarget:self action:@selector(onTimetableButtonPress:) forControlEvents:UIControlEventTouchUpInside];
}

[safariBtn setImage:btnImage forState:UIControlStateNormal];
[btnImage release];

}

Upvotes: 0

Lio
Lio

Reputation: 4282

Some things that seem to be wrong with your code:

[btnImage release] You've created that image using autorelease, you are not responsible for its release. This will most likely end up in weird stuff happening, i.e. what you encountered.

Are you sure that btnImage has contents after that if statement? is there a possibility that button is not equal to @"Browser" nor @"Timetable"?

Also, make sure the button is in its normal state.

Cheers

Upvotes: 2

Ravi Chokshi
Ravi Chokshi

Reputation: 1166

Try This ,

[safariBtn setBackgroundImage:btnImage forState:UIControlStateNormal];

No need to release btnImage ...

Upvotes: 2

PJR
PJR

Reputation: 13180

i think this should help you...

    [safariBtn setImage:[UIImage imageNamed:@"browser-button.png"] forState:UIControlStateNormal];

Upvotes: 0

Related Questions