带有4个按钮的应用程序 - 只想控制它们的行为 - 即按下按钮并在启用其他按钮时禁用该按钮。
从其他问题/答案中,我创建了以下.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;
和.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;
}
当我保存,构建,运行并按下从1开始按下按钮时,按下按钮1和2后按下它们后禁用;但是当我按3时它没有;此外,当我按4时,它不会启用1和2.
答案 0 :(得分:1)
您的逻辑对于您想要执行的操作是不正确的行为,您无法从同一发件人转换多个按钮并期望它们正常运行。使用您已有的属性并尝试:
- (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;
}
答案 1 :(得分:-2)
您要找的是Button。 userInteractionEnabled =否