我以编程方式创建了 5 个按钮,并互相给了标签,此代码块位于 for
环形。
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.tag= [[answersID objectAtIndex:m]integerValue];
button.frame = CGRectMake(760*i, 198, 760, 60);
UIImage *buttonImage = [UIImage imageNamed:@"Grey.png"];
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
[button addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
单击按钮后,所选按钮正在更改图像本身。
- (IBAction)clickButton:(id)sender{
buttonPlusID=[sender tag];
if ([sender isSelected]) {
[sender setImage:unselected forState:UIControlStateNormal];
//[sender setSelected:NO];
} else {
[sender setImage:selected forState:UIControlStateSelected];
[sender setSelected:YES];
}
但我想要这个。我有 5 个按钮,其中的图像是 gray.png 。我单击了一个按钮,按钮的图像更改了 green.png,然后我单击了另一个按钮,它将更改 green.png,而 green.png 将更改 gray.png。就像一个单选按钮。有什么办法吗?
对不起我的英语不好。谢谢你的兴趣。
最好的祝福。
最佳答案
是的,这是一件很常见的事情。我创建了自己的类来处理这种情况。
此类称为 ControlGroup,它的唯一职责是跟踪您添加到其中的所有 UIControl,并选择其中一个且只有一个。控件(在您的情况下为 UIButton)不需要了解彼此的任何信息,您可以拥有任意数量的控件。完成后不要忘记删除控件,因为此类将保留其元素。
这里是:
*.h 文件:
// This is a very simple class whose only purpose in life is to manage a group of
// UIControls in a way that only one of them is selected at any one time
@interface ControlGroup : NSObject
-(void)addControl:(UIControl*)control;
-(void)removeControl:(UIControl*)control;
-(UIControl*)currentlySelectedControl;
@end
*.m 文件:
#import "ControlGroup.h"
@interface ControlGroup ()
@property (nonatomic, strong) NSMutableSet *controls;
@end
@implementation ControlGroup
@synthesize controls = _controls;
-(id)init {
if ((self = [super init])) {
_controls = [[NSMutableSet alloc] init];
}
return self;
}
-(void)addControl:(UIControl*)control {
if (![self.controls containsObject:control]) {
[self.controls addObject:control];
[control addTarget:self action:@selector(controlTouched:) forControlEvents:UIControlEventTouchUpInside];
}
}
-(void)removeControl:(UIControl *)control {
if ([self.controls containsObject:control]) {
[control removeTarget:self action:@selector(controlTouched:) forControlEvents:UIControlEventTouchUpInside];
[self.controls removeObject:control];
}
}
-(void)controlTouched:(id)sender {
if ([sender isKindOfClass:[UIControl class]]) {
UIControl *selectedControl = (UIControl*)sender;
for (UIControl *control in self.controls) {
[control setSelected:FALSE];
}
[selectedControl setSelected:TRUE];
}
}
-(UIControl*)currentlySelectedControl {
UIControl *selectedControl = nil;
for (UIControl *control in self.controls) {
if ([control isSelected]) {
selectedControl = control;
break;
}
}
return selectedControl;
}
-(NSString*)description {
return [NSString stringWithFormat:@"ControlGroup; no. of elements: %d, elements: %@\n", self.controls.count, self.controls];
}
@end
希望这可以帮助!
编辑:如何使用这个类
您要做的第一件事是将它导入到您将要使用它的地方。在您的情况下,它将是您创建按钮的类:
1) 导入类
#import "ControlGroup.h"
然后你必须声明一个属性来保持对它的强引用
2) 在您的 *.h 文件中添加以下内容:
@property (nonatomic, strong) ControlGroup *controlGroup;
之后,在您的 init 方法中,您必须创建对象:
3) 在你的 init 方法中添加这个:
_controlGroup = [[ControlGroup alloc] init];
现在,您对可以使用的 ControlGroup 对象有了一个强引用。您要做的下一件事情是创建您的按钮。我相信你已经有了这一步。
4) 创建你的按钮。创建和配置按钮时,请使用
UIButton
的方法setImage:forState
并为 UIControlStateNormal
设置一张图片状态和另一个 UIControlStateSelected
状态。最后,对于您创建的每个按钮,您必须将其添加到
controlGroup
你拥有的对象。5) 将每个按钮添加到 ControlGroup 对象:
[self.controlGroup addControl:myButton];
尝试这些步骤,让我知道它对你有什么好处。
关于ios - 单选按钮,如单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18450740/