简体   繁体   中英

Objective-C Toggle Between Two Buttons

I'm new to Objective-C, and I'm trying to create an app with two buttons that toggle off and on in tandem. I've handled the button states (images for on and off) in Interace Bulder, but I'm having trouble figuring out exactly how to write the logic in Xcode.

Here are the conditions I need to fulfill:

-When neither button is on, either button may be turned on.

-When button1 is on, and button2 is clicked, button1 turns off, and button 2 turns on.

-When button2 is on, and button1 is clicked, button2 turns off, and button 1 turns on.

-When button1 is on and button1 is clicked, nothing happens.

-When button2 is on and button2 is clicked, nothing happens.

I've been using BOOL to try and work out the logic, but it's just not happening for me. Does anyone have an idea of how to do this?

The buttons were added programatiaclly, so the simple code looks like this in the .h file:

in .h:

#import <UIKit/UIKit.h>
@interface Profile_Settings_PageViewController : UIViewController {


IBOutlet UIButton *Button1;
IBOutlet UIButton *Button2;


BOOL ButtonSelected;
}

@property (nonatomic, retain) UIButton *Button1;
@property (nonatomic, retain) UIButton *Buton2;


-(IBAction) ButtonTouched:(id)sender;
@end

then the .m file:

#import "Profile_Settings_PageViewController.h"

@implementation Profile_Settings_PageViewController
@synthesize Button1;
@synthesize Button2;



-(IBAction) ButtonTouched:(id)sender
{

if (ButtonSelected == 0)
{
    [Button1 setSelected: NO];
    [Button2 setSelected: NO];
    ButtonSelected = 1;
}

else if (ButtonSelected == 1)  
{
    [Button1 setSelected: YES];
    [Button2 setSelected: YES];

    ButtonSelected = 0;

}

}
- (void)viewDidLoad {

[super viewDidLoad];

ButtonSelected == 0;
}

- (void)dealloc {
  [Button1 release];
  [Button2 release];

  [super dealloc];
}

@end

The issue I'm having is where the if statement begins. I'm not sure how to point to the specific buttons for the logic. I know what's there now is not correct both because it applies to both buttons, and because the logic is wrong, which creates problems when writing the conditional statements. I'm not sure how to fix it though...

If your image states are for NORMAL and SELECTED, create two buttons with outlets, tag them 1 & 2 respectively, and in your button action method:

Start with button 1 selected, button 2 normal.

- (IBAction)buttonAction:(UIButton*)sender 
{
    if (sender.selected)
        return;
    if (sender.tag == 1)
    {
        button1.selected = !button1.selected;
        button2.selected = !button1.selected;
    }
    else if (sender.tag == 2)
    {
        button2.selected = !button2.selected;
        button1.selected = !button2.selected;
    }   
}
int selectedIndex = -1;

if( selectedIndex != 1 && b1 clicked ) { selectedIndex = 1; do stuff }
if( selectedIndex != 2 && b2 clicked ) { selectedIndex = 2; do stuff }

ie:

  • have a "which button is selected" state
  • have a "nothing is selected" state
  • check each click against that selected/not-selected, and update it when things happen

Assuming you have figured out how to wire up your buttons in IB so that they call a method in your viewController when switched. Define two outlets in your view controller (one for each button) and an action to be called when the value of a switch changes:

@interface ToggleViewController : UIViewController {

  UISwitch *button1;
  UISwitch *button2;
}

@property (nonatomic, retain) IBOutlet UISwitch *button1;
@property (nonatomic, retain) IBOutlet UISwitch *button2;

- (IBAction)switchAction:(id)sender;

@end

Ensure you connect the outlets and also connect the Value Changed event to the switchAction for both buttons. The switch action method can then be something along these lines:

- (IBAction)switchAction:(id)sender {

  if ([sender isOn]) {

    if ([sender isEqual:button1])
    {
      [button2 setOn:NO animated:YES];
    } else {
      [button1 setOn:NO animated:YES];
    }
  }
}

For swift3 this is working

@IBAction func mealsButtonAction(_ sender: UIButton)
{

        if sender .isEqual(beforeMealsButton)
        {
            beforeMealsButton.isSelected = true
            beforeMealsButton .setImage(UIImage.init(named: "before_lunch_hover"), for: .normal)

            afterMealsButton .setImage(UIImage.init(named: "after_lunch_normal"), for: .normal)
            boolDict["bm"] = true
            boolDict["am"] = false
        }
        else
        {
            afterMealsButton.isSelected = true
            beforeMealsButton .setImage(UIImage.init(named: "before_lunch_normal"), for: .normal)

            afterMealsButton .setImage(UIImage.init(named: "after_lunch_hover"), for: .normal)
            boolDict["bm"] = false
            boolDict["am"] = true
        }


}

Thank you @Rog.

It's pretty simple: when either button is touched, you want to turn the other off. So in the UIControlEventTouchUpInside handler for button1, set button2 off. And in the UIControlEventTouchUpInside handler for button2, set button1 off.

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