gpt4 book ai didi

ios - 检测 UIButton iOS 的选定状态

转载 作者:行者123 更新时间:2023-11-29 02:43:33 25 4
gpt4 key购买 nike

如何检测 uibutton 的选定状态?

我有 7 个按钮,我让它们能够一次切换或选择多个按钮。

我希望在按下完成按钮时能够知道哪些按钮处于选中状态。

因此,如果选择了 M、T 和 W,那么我希望能够在推送完成时检测到这一点。

我目前在按钮上放置一个标签,然后调用一个方法来取消选择或选择多个按钮。

self.repeatOccurrenceFrequencyWeeklyTF = [[UITextField alloc]init];
self.repeatOccurrenceFrequencyWeeklyTF.frame = CGRectMake(80, 80, 32, 32);
self.repeatOccurrenceFrequencyWeeklyTF.delegate = self;
self.repeatOccurrenceFrequencyWeeklyTF.background = [UIImage imageNamed:@"repeatWeekly"];
self.repeatOccurrenceFrequencyWeeklyTF.font = [UIFont fontWithName:@"SegoeWP" size:15];
self.repeatOccurrenceFrequencyWeeklyTF.textColor = [UIColor appGreyText];
[self.repeatOccurrenceFrequencyWeeklyTF setValue:[UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0 alpha:1.0] forKeyPath:@"_placeholderLabel.textColor"];
self.repeatOccurrenceFrequencyWeeklyTF.placeholder = @"1";
UIView *leftView1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 12, self.repeatOccurrenceFrequencyWeeklyTF.frame.size.height)];
self.repeatOccurrenceFrequencyWeeklyTF.leftView = leftView1;
self.repeatOccurrenceFrequencyWeeklyTF.leftViewMode = UITextFieldViewModeAlways;
self.repeatOccurrenceFrequencyWeeklyTF.rightViewMode = UITextFieldViewModeAlways;
self.keyboardToolbar = [self createInputToolbar];
self.repeatOccurrenceFrequencyWeeklyTF.inputAccessoryView = self.keyboardToolbar;
self.repeatOccurrenceFrequencyWeeklyTF.delegate = self;
self.repeatOccurrenceFrequencyWeeklyTF.keyboardType = UIKeyboardTypeNumberPad;
self.repeatOccurrenceFrequencyWeeklyTF.enabled = NO;
[self.view addSubview:self.repeatOccurrenceFrequencyWeeklyTF];

// Now, in your button action handler, you can do something like this:
- (void)mondayButtonTouch:(UIButton *)aButton withEvent:(UIEvent *)event
{
aButton.selected = !aButton.selected;

if(aButton.tag == 111) {

}

if(aButton.tag == 222) {

}

if(aButton.tag == 333) {

}

if(aButton.tag == 444) {

}

if(aButton.tag == 555) {

}

if(aButton.tag == 666) {

}

NSLog(@"dsfdfdfsdfs %ld", (long)aButton.tag);
[aButton setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];

}

最佳答案

我会使用一个 NS_ENUM(这有助于保持一个漂亮且可读的代码)和一个 NSMutableArray 来跟踪您选择的按钮。声明一个看起来像这样的枚举:

typedef NS_ENUM(NSInteger, Weekday) {
WeekdayMonday,
WeekdayTuesday,
WeekdayWednesday,
WeekdayThursday,
WeekdayFriday,
WeekdaySaturday,
WeekdaySunday
};

然后用正确的枚举标记你的按钮:

tuesdayButton.tag = WeekdayTuesday;

并在您点击按钮时检查您的枚举是否存在于您的数组中:

- (void)buttonTouch:(UIButton *)aButton withEvent:(UIEvent *)event
{
if ([array containsObject:@(aButton.tag)]){ //exists, remove it from array
[array removeObjectIdenticalTo:@(aButton.tag)];
}
}else{
[array addObject:@(aButton.tag)];
}

}

关于ios - 检测 UIButton iOS 的选定状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25423146/

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