bkb*_*abs 1 iphone select objective-c uibutton
如何制作这些按钮,以便一次只能使用一个?我运行顺便说一句,我现在没有得到任何错误.我只是在寻找解决方案来解决我的挑战.谢谢你的帮助
它们是在for循环中生成的,如下所示:
for (int l=0; l<list.length; l++) {
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[aButton setTag:l];
CGRect buttonRect = CGRectMake(11+charact*20, -40 + line*50, 18, 21);
aButton.frame = buttonRect;
[aButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[aButton setTitle:@" " forState:UIControlStateNormal];
[gameScroll addSubview:aButton];
}
Run Code Online (Sandbox Code Playgroud)
然后单击按钮时的操作是:
- (void) buttonClicked:(UIButton *)sender {
int tag = sender.tag;
if (sender.selected == TRUE) {
[sender setSelected:FALSE];
[sender setBackgroundColor:[UIColor clearColor]];
}
else if (sender.selected == FALSE) {
[sender setSelected:TRUE];
[sender setBackgroundColor:[UIColor redColor]];
}
}
Run Code Online (Sandbox Code Playgroud)
现在一切正常但我希望它知道是否已经选择了一个按钮并取消选择其他按钮,或者在用户点击该按钮范围之外的任何时候自动取消选择
提前致谢
我建议在按钮初始化时将所有按钮放到Array中
NSMutableArray* buttons = [NSMutableArray arrayWithCapacity: list.length];
for (int l=0; l<list.length; l++) {
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[aButton setTag:l];
CGRect buttonRect = CGRectMake(11+charact*20, -40 + line*50, 18, 21);
aButton.frame = buttonRect;
[aButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[aButton setTitle:@" " forState:UIControlStateNormal];
[gameScroll addSubview:aButton];
[buttons addObject: aButton];
}
Run Code Online (Sandbox Code Playgroud)
每次按钮点击触发,然后执行你的逻辑:
for (UIButton* button in buttons)
{
if (button != sender)
{
[button setSelected: FALSE];
[button setBackgroundColor:[UIColor redColor]];
}
}
int tag = sender.tag;
if (sender.selected == TRUE) {
[sender setSelected:FALSE];
[sender setBackgroundColor:[UIColor clearColor]];
}
else if (sender.selected == FALSE) {
[sender setSelected:TRUE];
[sender setBackgroundColor:[UIColor redColor]];
}
Run Code Online (Sandbox Code Playgroud)
希望有帮助:)
归档时间: |
|
查看次数: |
4326 次 |
最近记录: |