Reputation: 10959
I want to change button background image as per the web service response. if in response I get unread messages to true then image will different and if there is no unread messages image will be different. I am getting the response very well but not able to change my button's background image.
This is the snippet I am using
if(unread>0){
[badgeTitle setBackgroundImage:[UIImage imageNamed:@"unread.png"] forState:UIControlStateNormal];
}
else{
[badgeTitle setBackgroundImage:[UIImage imageNamed:@"read.png"] forState:UIControlStateNormal];
}
Upvotes: 4
Views: 8992
Reputation: 552
The solution can be very simple, if you look at it. Keep a global UIButton and not a local one. This helps, as when the web request returns back a response, the button needs to be present in-scope and not get removed from the memory.
So use,
self.myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.myButton setBackgroundImage:[UIImage imageNamed:@"unread.png"] forState:UIControlStateNormal];
OR
[self.myButton setImage:[UIImage imageNamed:@"unread.png"] forState:UIControlStateNormal];
And, if you checking on the click of a button, then follow the steps :
-(IBAction)checkForMsgCount: (id)sender
{
UIButton *buttonObj = (UIButton*)sender;
[buttonObj setBackgroundImage:[UIImage imageNamed:@"unread.png"] forState:UIControlStateNormal];
}
This changes the background image, for the button that is responding to the function.
Upvotes: 7
Reputation: 1663
[YourBtn setImage:[UIImage imageNamed:@"your.png"] forState:UIControlStateNormal];
try it please.
Can you also try this for check it out if you really have this pictures in your bundle ?
NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
NSString *imagePath = [[NSString alloc] initWithFormat:@"%s/yourButton.png", [bundlePath UTF8String]];
if ([[NSFileManager defaultManager] fileExistsAtPath:imagePath]) {
NSLog(@"image exists");
}
else{
NSlog(@"image Does Not Exist");
}
Upvotes: 1
Reputation: 462
UIButton *btn = [[UIButton alloc]init];
btn.frame = CGRectMake(50, 50, 100, 50);
[btn setTitle:@"ok" forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:@"icon.png"] forState:UIControlStateNormal];
btn.backgroundColor=[UIColor blueColor];
I hope this works for u....
Upvotes: 0
Reputation:
please try code below
if(unread>0){
[badgeTitle setBackgroundImage:[UIImage imageNamed:@"unread.png"] forState:UIControlStateNormal];
[badgeTitle setBackgroundImage:[UIImage imageNamed:@"unread.png"] forState :UIControlStateSelected];
}
else{
[badgeTitle setBackgroundImage:[UIImage imageNamed:@"read.png"] forState:UIControlStateNormal];
[badgeTitle setBackgroundImage:[UIImage imageNamed:@"read.png"] forState :UIControlStateSelected];
}
Upvotes: 1
Reputation: 31073
First check IBOutlet connection of your button :)
And set your code after getting data from web service i.e. after getting value of unread
if(unread>0){
[badgeTitle setBackgroundImage:[UIImage imageNamed:@"unread.png"] forState:UIControlStateNormal];
}
else{
[badgeTitle setBackgroundImage:[UIImage imageNamed:@"read.png"] forState:UIControlStateNormal];
}
Upvotes: 0
Reputation: 31339
Set background images for UIControlStateNormal and UIControlStateSelected as separate lines.
if(unread>0){
[badgeTitle setBackgroundImage:[UIImage imageNamed:@"unread.png"] UIControlStateNormal];
[badgeTitle setBackgroundImage:[UIImage imageNamed:@"unread.png"] UIControlStateSelected];
} else {
[badgeTitle setBackgroundImage:[UIImage imageNamed:@"read.png"] UIControlStateNormal];
[badgeTitle setBackgroundImage:[UIImage imageNamed:@"read.png"] UIControlStateSelected];
}
Upvotes: 0
Reputation: 20021
[badgeTitle setBackgroundImage:[UIImage imageNamed:@"unread.png"] forState:UIControlStateNormal|UIControlStateSelected];
are you adding the button image for UIControlStateNormal
or UIControlStateSelected
try
[badgeTitle setBackgroundImage:[UIImage imageNamed:@"unread.png"] forState:UIControlStateNormal];
find whether the image exist and added properly to the bundle and The name is correct
Upvotes: 0