Objective-C在两个按钮之间切换

时间:2021-05-19 21:28:07

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.

我是Objective-C的新手,我正在尝试创建一个带有两个按钮的应用程序,它们可以串联切换。我已经处理了Interace Bulder中的按钮状态(打开和关闭图像),但我无法确定如何在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.

- 当按钮1打开且单击按钮2时,按钮1关闭,按钮2打开。

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

- 当按钮2打开且单击按钮1时,按钮2关闭,按钮1打开。

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

- 当button1打开并单击button1时,没有任何反应。

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

- 当button2打开并单击button2时,没有任何反应。

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?

我一直在使用BOOL试图找出逻辑,但这对我来说并没有发生。有没有人知道如何做到这一点?

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

按钮是以编程方式添加的,因此简单代码在.h文件中如下所示:

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:

那么.m文件:

#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语句的开始。我不确定如何指向逻辑的特定按钮。我知道现在有什么不正确,因为它适用于两个按钮,并且因为逻辑错误,这在编写条件语句时会产生问题。我不知道怎么解决它...

5 个解决方案

#1


4  

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:

如果您的图像状态为NORMAL和SELECTED,请创建两个带插座的按钮,分别标记1和2,并在按钮操作方法中:

Start with button 1 selected, button 2 normal.

选择按钮1,按钮2正常开始。

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

#2


1  

int selectedIndex = -1;

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

i.e.:

  • 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
  • 检查每次单击是否选中/未选中,并在发生事件时更新它

#3


1  

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:

假设您已经弄清楚如何在IB中连接按钮,以便在切换时调用viewController中的方法。在视图控制器中定义两个出口(每个按钮一个),以及当开关值更改时要调用的动作:

@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:

确保连接插座并将Value Changed事件连接到两个按钮的switchAction。然后,切换操作方法可以是以下几点:

- (IBAction)switchAction:(id)sender {

  if ([sender isOn]) {

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

#4


0  

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.

这非常简单:当触摸任一按钮时,您想要关闭另一个按钮。因此,在button1的UIControlEventTouchUpInside处理程序中,将button2设置为off。在button2的UIControlEventTouchUpInside处理程序中,将button1设置为off。

#5


0  

For swift3 this is working

对于swift3,这是有效的

@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.

谢谢@Rog。

#1


4  

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:

如果您的图像状态为NORMAL和SELECTED,请创建两个带插座的按钮,分别标记1和2,并在按钮操作方法中:

Start with button 1 selected, button 2 normal.

选择按钮1,按钮2正常开始。

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

#2


1  

int selectedIndex = -1;

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

i.e.:

  • 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
  • 检查每次单击是否选中/未选中,并在发生事件时更新它

#3


1  

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:

假设您已经弄清楚如何在IB中连接按钮,以便在切换时调用viewController中的方法。在视图控制器中定义两个出口(每个按钮一个),以及当开关值更改时要调用的动作:

@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:

确保连接插座并将Value Changed事件连接到两个按钮的switchAction。然后,切换操作方法可以是以下几点:

- (IBAction)switchAction:(id)sender {

  if ([sender isOn]) {

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

#4


0  

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.

这非常简单:当触摸任一按钮时,您想要关闭另一个按钮。因此,在button1的UIControlEventTouchUpInside处理程序中,将button2设置为off。在button2的UIControlEventTouchUpInside处理程序中,将button1设置为off。

#5


0  

For swift3 this is working

对于swift3,这是有效的

@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.

谢谢@Rog。