4

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];
}
KDeogharkar
  • 10,939
  • 7
  • 51
  • 95

8 Answers8

7

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.

KDeogharkar
  • 10,939
  • 7
  • 51
  • 95
Hitesh
  • 542
  • 3
  • 12
  • that doesnt make any sense and I debug for so many time if button is removed or not but it is there all the time. – KDeogharkar Jul 26 '13 at 10:46
  • the solution indicates clearly, that you need to be clear, as to WHAT button is responding to the method. This can be used by, type casting the sender object, to a UIButton, and then changing the image to it. TRY THIS and TELL IF IT DOES NOT WORK – Hitesh Jul 26 '13 at 11:39
  • I have myself used this. Telling you, it will work. Just try it ! – Hitesh Jul 26 '13 at 11:41
1
[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");
      }
Yucel Bayram
  • 1,653
  • 2
  • 22
  • 41
1

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];
}
9to5ios
  • 5,319
  • 2
  • 37
  • 65
  • He wanted to change it for both UIControlState selected and normal. You should call the method twice to add the image for these two control states. Also, you should give some explanation for this code and for what the user was doing wrong. – Ramy Al Zuhouri Jul 26 '13 at 10:42
  • yes it will work please check if you only want to change only for UIControlStateNormal it will work –  Jul 26 '13 at 10:50
  • why dont you put forstate in second line? – KDeogharkar Jul 26 '13 at 10:55
  • oh it just syntax error please add this when you apply this code –  Jul 26 '13 at 11:03
0

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];
}
Rajneesh071
  • 30,846
  • 15
  • 61
  • 74
0
[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

Lithu T.V
  • 19,955
  • 12
  • 56
  • 101
0

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];
}
Shamsudheen TK
  • 30,739
  • 9
  • 69
  • 102
0
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....

Maulik Kundaliya
  • 452
  • 3
  • 17
  • try this, if(unread.text.length>0){ [badgeTitle setBackgroundImage:[UIImage imageNamed:@"unread.png"] forState:UIControlStateNormal]; } else{ [badgeTitle setBackgroundImage:[UIImage imageNamed:@"read.png"] forState:UIControlStateNormal]; } if unread is string then this is works... – Maulik Kundaliya Jul 26 '13 at 12:43
0

after setting the background image

[badgeTitle setNeedsLayout];
tdios
  • 225
  • 1
  • 2
  • 16