UIButton 按下,另一个不按下

Posted

技术标签:

【中文标题】UIButton 按下,另一个不按下【英文标题】:UIButton pressed, other one doesn't press 【发布时间】:2014-07-11 15:19:11 【问题描述】:

嗨,我有一些代码可以创建两个按钮,当按下按钮时,会设置一个触发器,它会更改背景和文本颜色。

我怎样才能做到一旦按下一个就不能按下另一个。

这会创建按钮

//learnmore button
UIButton *learnmorebutton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.view addSubview:learnmorebutton];
learnmorebutton.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.3];
learnmorebutton.frame = CGRectMake(0.0, 518.0, 159.0, 50.0);
[learnmorebutton setTitle:@"learn more" forState:UIControlStateNormal];
[learnmorebutton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
learnmorebutton.titleLabel.font = [UIFont fontWithName:@"Helvetica Light" size:17.0];


//signup button
UIButton *signup = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[self.view addSubview:signup];
signup.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.3];
signup.frame = CGRectMake(162.0, 518.0, 160.0, 50.0);
[signup setTitle:@"sign up" forState:UIControlStateNormal];
signup.titleLabel.font = [UIFont fontWithName:@"Helvetica Light" size:17.0];
signup.titleLabel.textColor = [UIColor whiteColor];

这是两个触发器

   [learnmorebutton addTarget:self action:@selector(learnMoreClickEvent:) forControlEvents:UIControlEventTouchUpInside];
    [signup addTarget:self action:@selector(signupClickEvent:) forControlEvents:UIControlEventTouchUpInside];

这是两个事件

- (void)learnMoreClickEvent:(UIButton *)sender

    //sender is the button that was tapped
    [sender setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    sender.backgroundColor = [UIColor whiteColor];




- (void)signupClickEvent:(UIButton *)sender

    //sender is the button that was tapped
    [sender setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    sender.backgroundColor = [UIColor whiteColor];



谢谢大家...

【问题讨论】:

【参考方案1】:

您首先要保留对按钮的引用,最好是创建这些按钮的 viewController 上的属性。

@interface MyViewController : UIViewController

@property (nonatomic, strong) UIButton *learnmorebutton;
@property (nonatomic, strong) UIButton *signup;

@end;

然后在方法中禁用按钮:

<button>.enabled = NO:

或者隐藏它:

<button>.hidden = YES:

【讨论】:

我已经这样做了,但在 VOID(发件人)内。我可以选择按钮 self.learnmorebutton 但它不起作用。但是 learnmorebutton.enabled = NO 确实加载了视图中的作品【参考方案2】:

就像 rckoenes 提到的那样,您可以将这些 UIButton 声明为属性并执行此操作.. 或

这样分配标签

//learnmore button
UIButton *learnmorebutton = [UIButton buttonWithType:UIButtonTypeCustom];
learnmorebutton.tag = 10;
learnmorebutton.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.3];
learnmorebutton.frame = CGRectMake(0.0, 518.0, 159.0, 50.0);
[learnmorebutton setTitle:@"learn more" forState:UIControlStateNormal];
[learnmorebutton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
learnmorebutton.titleLabel.font = [UIFont fontWithName:@"Helvetica Light" size:17.0];
[learnmorebutton addTarget:self action:@selector(learnMoreClickEvent:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:learnmorebutton];

//signup button
UIButton *signup = [UIButton buttonWithType:UIButtonTypeRoundedRect];
signup.tag = 20;
signup.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.3];
signup.frame = CGRectMake(162.0, 518.0, 160.0, 50.0);
[signup setTitle:@"sign up" forState:UIControlStateNormal];
signup.titleLabel.font = [UIFont fontWithName:@"Helvetica Light" size:17.0];
signup.titleLabel.textColor = [UIColor whiteColor];
[self.view addSubview:signup];
[signup addTarget:self action:@selector(signupClickEvent:) forControlEvents:UIControlEventTouchUpInside];

通过使用标签访问按钮

- (void)learnMoreClickEvent:(UIButton *)sender

    UIButton *signUp = (UIButton*)[self.view viewWithTag:20]
    signUp.enabled = NO;
    //sender is the button that was tapped
    [sender setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    sender.backgroundColor = [UIColor whiteColor];


- (void)signupClickEvent:(UIButton *)sender

    UIButton *learnmorebutton = (UIButton*)[self.view viewWithTag:10]
    learnmorebutton.enabled = NO;
   //sender is the button that was tapped
   [sender setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
   sender.backgroundColor = [UIColor whiteColor];

希望这能给你一个想法。 基本上,如果您正在寻找切换,您可以随时使用 UISegementControl。我的意思是这取决于您的要求。

【讨论】:

【参考方案3】:

将要禁用的按钮的启用属性设置为 NO:&lt;button&gt;.enabled = NO;

【讨论】:

以上是关于UIButton 按下,另一个不按下的主要内容,如果未能解决你的问题,请参考以下文章

按下时使用 UIViewAnimationTransitionFlipFromRight 翻转 UIButton 并在“另一侧”显示新按钮

如何使 UIButton 与 UIScrollView 一起移动

如何停止多个 UIButton 按下?

按下其他按钮后无法更改uibutton的图像

斯威夫特,触摸 UIButton 并触摸另一个

如何在不按下按钮的情况下自动处理 <textarea> 的内容?