Thursday, May 30, 2013

UIButton imageForState example in Objective C (iOS).

UIButton imageForState

Returns the image used for a button state.

- (UIImage *)imageForState:(UIControlState)state

Parameters
state
The state that uses the image. Possible values are described in UIControlState.

Return Value of [UIButton imageForState]
The image used for the specified state.

UIButton imageForState example.
UIImage *img = [button imageForState:UIControlStateNormal];

Example of [UIButton imageForState].
-(void)buttonTouched:(id)sender
{
 UIButton *btn = (UIButton *)sender;

 if( [[btn imageForState:UIControlStateNormal] isEqual:[UIImage imageNamed:@"icon-Locked.png"]])
    {
       [btn setImage:[UIImage imageNamed:@"icon-Unlocked.png"] forState:UIControlStateNormal];
       // other statements
    }
 else
   {
       [btn setImage:[UIImage imageNamed:@"icon-Locked.png"] forState:UIControlStateNormal];
       // other statements
   }
}

UIButton imageForState example.
UIButton *buttonClicked = (UIButton *)sender;
UIImage *imageOfClicked = [buttonClicked imageForState:UIControlStateHighlighted];
[buttonClicked setImage:imageOfClicked forState:UIControlStateNormal];

End of UIButton imageForState example article.