How do I use a button to toggle between highlighted in non-highlighted images in a UIImageView object?

I am trying to use one button to toggle between two images in a UIImageView object. I've been searching around for a problem/answer close to my specific problem but have had no luck, so here goes...

 IBOutlet UIButton *selectionButton;
 UIImageView *selectionStatus;
 @property(nonatomic, retain) UIButton    *selectionButton;
 @property(nonatomic, retain) UIImageView *selectionStatus;
 - (IBAction)selectionButtonPressed: (id)sender;

 @synthesize selectionButton;
 @synthesize selectionStatus;
 - (IBAction)selectionButtonPressed: (id)sender
   if (selectionStatus.highlighted == YES)  
      selectionStatus.highlighted = NO;
      selectionStatus.highlighted = YES;

 -(void)viewWillAppear:(BOOL)animated {
   // selection images
   selectionStatus = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"NotSelected.png"]  
                                       highlightedImage:[UIImage imageNamed:@"Selected.png"]];

When I run this in the simulator, the images don't appear in "SelectionStatus", so I think I'm missing something big. Help would be very much appreciated. Thanks!

I should have said more about the button. selectionButton is a custom button that overlays the selectionStatus imageview as well as a label that displays the results of a calculation. Tapping selectionButton selects the value in the label for inclusion in a calculation of an average of up to three selected label values. The image displayed in selectionStatus is a checkmark that changes colors depending on the selection status. I've found another approach that works, but doesn't seem as elegant:

- (IBAction)selectionButtonPressed: (id)sender
    if (toggleValue == YES) {
        toggleValue = NO;
        UIImage *unselect = [UIImage imageNamed:@"NotSelected.png"];
        selectionStatus.image = unselect;
    else {
        toggleValue = YES;
        UIImage *select = [UIImage imageNamed:@"Selected.png"];
        selectionStatus.image = select;

This approach does not require the use of viewWillAppear, and initialization is done in viewDidLoad. Thanks for your comments and suggestions.


In ib , set the button's background image property.I think your image is very big,in fact it is show on your button when you set your button's state into highlight. Just try,I am not sure.

Need Your Help

Passing a DefaultHttpClient between activites

android object

I am trying to maintain my session variables that I get back in a cookie from my website in my DefaultHttpClient by passing the client between activities. Simply put how can I pass this object bet...

Using nhibernate <loader> element with HQL queries

c# nhibernate nhibernate-mapping hql

I'm attempting to use an HQL query in a &lt;loader&gt; element to load an entity based on other entities.