检测UIButton iOS的选定状态

时间:2014-08-21 09:45:48

标签: ios objective-c iphone uibutton

如何检测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];

}

4 个答案:

答案 0 :(得分:1)

我会使用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)];
}

}

答案 1 :(得分:0)

可能是创建名为NSMutableArray的{​​{1}}。这样做:

selectedButton

现在点击- (void)mondayButtonTouch:(UIButton *)aButton withEvent:(UIEvent *)event { aButton.selected = !aButton.selected; if(!aButton.selected && selectedButton.containsObject(aButton.Tag)) { [selectedButton removeObject:aButton.tag]; } else if(aButton.selected && !selectedButton.containsObject(aButton.Tag)) { [selectedButton addObject:aButton.tag]; } // do your stuff here } 按钮,您可以使用done数组跟踪选定标签的所有按钮。

您可以使用:

selectedButton

答案 2 :(得分:0)

你可以用这个:

for (UIButton *btn in [self.view subviews]) { // self.view (change it with your button superview)
    if ([btn isKindOfClass:[UIButton class]] && [btn isSelected] == YES) {
        // here you found the button which is selected
    }
}

答案 3 :(得分:0)

[self.view viewWithTag:yourTagHere]替换