gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 22:02:50 24 4
gpt4 key购买 nike

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

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

这将创建按钮

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com