ios - UIButton 按下,另一个不按下

标签 ios objective-c xcode uibutton

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

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

这将创建按钮

//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];


}

谢谢大家...

最佳答案

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

@interface MyViewController : UIViewController

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

@end;

然后在方法中禁用按钮:

<button>.enabled = NO:

或者隐藏它:

<button>.hidden = YES:

关于ios - UIButton 按下,另一个不按下,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24701126/

相关文章:

ios - 如何快速将 NSURL 转换为 CFURL?

iOS NSFetchedResultsController : Is it possible to use same cache for the same fetch in different View Controllers?

ios - 旋转设备时是否可以对 UIView 使用自定义旋转?

ios - Xcode 按钮图像不显示

ios - Phonegap build - 将远程文件下载到 iOS 上的文档目录

ios - 如何实现占位符文本在 UITextField 中逐字符消失

ios - 使用 AFNetworking 将文本和多张图片发布到 Google Blobstore

ios - 'Apple Developer Program License Agreement' 已更新

ios - 如何从终端启动 iOS 模拟器?

ios - 如何访问字典数组并在 Objective C 中使用字典对象?