简体   繁体   English

Xcode 5.1-按钮禁用/启用不起作用?

[英]Xcode 5.1 - Button Disable/Enable not working?

App with 4 buttons - just want to control their behavior - ie push a button and it is disabled while others are enabled. 带有4个按钮的应用-只想控制其行为-即按下一个按钮,则在启用其他按钮时将其禁用。

From other questions/answers here, I've created the following .h entries: 根据此处的其他问题/答案,我创建了以下.h条目:

@interface TestButtonsViewController : UIViewController
- (IBAction)b1:(id)sender;
- (IBAction)b2:(id)sender;
- (IBAction)b3:(id)sender;
- (IBAction)b4:(id)sender;

@property (retain, nonatomic) IBOutlet UIButton *b1;
@property (retain, nonatomic) IBOutlet UIButton *b2;
@property (retain, nonatomic) IBOutlet UIButton *b3;
@property (retain, nonatomic) IBOutlet UIButton *b4;

and the .m 还有他们

- (IBAction)b1:(id)sender {

UIButton *b1 = (UIButton *) sender;
b1.enabled = NO;
UIButton *b2 = (UIButton *) sender;
b2.enabled = YES;
UIButton *b3 = (UIButton *) sender;
b3.enabled = NO;
UIButton *b4 = (UIButton *) sender;
b4.enabled = NO;
}

- (IBAction)b2:(id)sender {

UIButton *b1 = (UIButton *) sender;
b1.enabled = NO;
UIButton *b2 = (UIButton *) sender;
b2.enabled = NO;
UIButton *b3 = (UIButton *) sender;
b3.enabled = YES;
UIButton *b4 = (UIButton *) sender;
b4.enabled = NO;
}

- (IBAction)b3:(id)sender {

UIButton *b1 = (UIButton *) sender;
b1.enabled = NO;
UIButton *b2 = (UIButton *) sender;
b2.enabled = NO;
UIButton *b3 = (UIButton *) sender;
b3.enabled = NO;
UIButton *b4 = (UIButton *) sender;
b4.enabled = YES;
}

- (IBAction)b4:(id)sender {

UIButton *b1 = (UIButton *) sender;
b1.enabled = YES;
UIButton *b2 = (UIButton *) sender;
b2.enabled = YES;
UIButton *b3 = (UIButton *) sender;
b3.enabled = YES;
UIButton *b4 = (UIButton *) sender;
b4.enabled = NO;
}

When I save, build, run and push the buttons starting at 1 and going down the buttons 1 and 2 do disable after I push them; 当我保存,构建,运行并按下从1开始的按钮,然后按下按钮1和2时,它们将被禁用; but when I press 3 it doesn't; 但是当我按3时却没有; furthermore when I press 4 it doesn't enable 1 and 2. 此外,当我按4时,它不会启用1和2。

Your logic is incorrect behavior for what you want to do, you can't cast multiple buttons from the same sender and expect them to behave properly. 您的逻辑对您要执行的操作来说是不正确的行为,您不能从同一发送者投下多个按钮,并期望它们能正常运行。 Use your properties you already have instead and try this: 使用您已经拥有的属性,然后尝试以下操作:

- (IBAction)b1:(id)sender 
{
    self.b1.enabled = NO;
    self.b2.enabled = YES;
    self.b3.enabled = NO;
    self.b4.enabled = NO;
}

- (IBAction)b2:(id)sender 
{
    self.b1.enabled = NO;
    self.b2.enabled = NO;
    self.b3.enabled = YES;
    self.b4.enabled = NO;
}

- (IBAction)b3:(id)sender 
{
    self.b1.enabled = NO;
    self.b2.enabled = NO;
    self.b3.enabled = NO;
    self.b4.enabled = YES;
}

- (IBAction)b4:(id)sender 
{
    self.b1.enabled = YES;
    self.b2.enabled = YES;
    self.b3.enabled = YES;
    self.b4.enabled = NO;
}

What you are looking for is Button. 您正在寻找的是Button。 userInteractionEnabled = NO userInteractionEnabled =否

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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