Petahwil
Petahwil

Reputation: 437

Turn a button on from another button in objective C

How would I go about turning the first button back on from the second button [self firstButton:sender.enabled = Yes]; only works to turn it off. Which the compiler gives me a warning about turning the button off that way. So if there is a better way to turn it off from the second button to please let me know how to turn it on and off..

-(IBAction)firstButton:(UIButton *)sender{
if(firstButton is clicked){
//Turn firstButton off
((UIButton *)sender).enabled = NO;
}
}

-(IBAction)secondButton:(UIButton *)sender{
if(secondButton is clicked)
{
//Turn firstButton BACK ON?
[self firstButton:sender.enabled = Yes];
}
}

Thanks!!

Upvotes: 1

Views: 878

Answers (2)

Joel
Joel

Reputation: 16124

You can set ivars for each button using IBOutlet

IBOutlet UIButton *firstButton, *secondButton;

Then link them to the correct button in Interface Builder.

Then you can use these IBActions to accomplish what you're looking for.

-(IBAction)firstButtonClicked:(id)sender
{
    [firstButton setEnabled:NO];
}

-(IBAction)secondButtonClicked:(id)sender
{
    [firstButton setEnabled:YES];
}

Upvotes: 2

Mick MacCallum
Mick MacCallum

Reputation: 130183

Are you trying to toggle the state of the other button from each button? If so, this should help. First use viewWithTag: to get a reference to the other button. This will require that you properly assign the tag property of each button to the same tags as specified below. Then, you can simply set the buttons enabled state to ! its current enabled state. Which will toggle it. on to off, off to on.

-(IBAction)firstButton:(UIButton *)sender
{
    UIButton *otherButton = (UIButton *)[self.view viewWithTag:33];
    [otherButton setEnabled:!otherButton.enabled];
}

-(IBAction)secondButton:(UIButton *)sender
{
    UIButton *otherButton = (UIButton *)[self.view viewWithTag:44];
    [otherButton setEnabled:!otherButton.enabled];
}

Upvotes: 1

Related Questions