开发者

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.


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


Try This ,

[safariBtn setBackgroundImage:btnImage forState:UIControlStateNormal];

No need to release btnImage ...


i think this should help you...

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


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];

}


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜