setImage:forState: does not work in iOS 3.2

835 Views Asked by At

The expected behavior is to change the image (from play to pause and from pause to play) when the play action is clicked. This works as expected in version 4.x. In version 3.2 the setImage does not work meaning the setImage does not set the image. Call to imageForState:UIControlStateNormal returns null in 3.2 version. Any ideas?

@interface TestViewController : UIViewController {
  IBOutlet UIButton *playButton;
  BOOL play;
}

- (IBAction) play:(id)sender;

@end


@implementation TestViewController

- (IBAction) play:(id)sender {  
  if (play == YES) {
    [playButton setImage:[UIImage imageNamed:@"pause"] forState:UIControlStateNormal];    
   play = NO;
  } else {
    [playButton setImage:[UIImage imageNamed:@"play"] forState:UIControlStateNormal];    
    play = YES;
  }
  //[playButton imageForState:UIControlStateNormal]

}

@end
2

There are 2 best solutions below

0
Andrew On

Your code looks correct, have you checked that you connected up the IBOutlet in your XIB file for your button?

0
Saurabh Passolia On

UIImage's imageNamed takes in a image name with extension i.e if you change your code

[UIImage imageNamed:@"pause"]

to

[UIImage imageNamed:@"pause.png"] or [UIImage imageNamed:@"pause.jpg"]

your code will work as per your expectations.