Ted
Ted

Reputation: 3875

Changing a UIButton to be turned off

I have a simple button with two images for each UIControlStates. I want this button to behave as a control and as indicator so that I can press it down - "activate" and press again "deactivate", While some other functions can cause it to be turned off ("deactivate"). My question is how can I change it's state from selected to not-selected?
Changing the selected property would not do the trick :)

Upvotes: 0

Views: 237

Answers (2)

Misha
Misha

Reputation: 5380

Simplest way would be consider button as switch and change it's state according switch on/off. For example use BOOL variable which upon button touch gets its value YES or NO while according to it button gets its image.

- (void) buttonTouched:(id)sender
{
    switchOn = !switchOn;
    UIImage *buttonImage = nil; 

    if (switchOn == YES) 
    { 
        buttonImage = [UIImage imageNamed:@"on.png"]; 
    }
    else
    {
        buttonImage = [UIImage imageNamed:@"off.png"]; 
    }
    [myButton setImage:buttonImage forState:UIControlStateNormal];
    [myButton setImage:buttonImage forState:UIControlStateSelected];
}

if you need to programmatically set button "disabled":

- (void) setButtonDisabled
{
    switchOn = YES; //calling buttonTouched: will turn it to NO
    [self buttonTouched:nil];
}

Upvotes: 0

ask4asif
ask4asif

Reputation: 676

You can disable the button so it can't be press able.

yourButton.enabled = NO;

and When you want it back to be press able, then you can enabled it

yourButton.enabled = YES;

Upvotes: 1

Related Questions