简体   繁体   中英

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!!

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];
}

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];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM