Iphone UIButton复选框
我有6个UIButton,它们是在以下代码中生成的:Iphone UIButton复选框,iphone,ios5,uibutton,Iphone,Ios5,Uibutton,我有6个UIButton,它们是在以下代码中生成的: int count = 0; for (int i= 0; count<6; i++) { for (int j=0; j<1&&count<6; j++) { followbutn = [UIButton buttonWithType:UIButtonTypeCustom]; followbutn.frame = CGRectMake(
int count = 0;
for (int i= 0; count<6; i++) {
for (int j=0; j<1&&count<6; j++) {
followbutn = [UIButton buttonWithType:UIButtonTypeCustom];
followbutn.frame = CGRectMake(20, i*44+53, 280, 40);
[followbutn setTitle:[followuparray objectAtIndex:count] forState:UIControlStateNormal];
[followbutn addTarget:self action:@selector(followaction:) forControlEvents:UIControlEventTouchUpInside];
[followbutn setImage:[UIImage imageNamed:@"checkbox_unchecked.png"] forState:UIControlStateNormal];
followbutn.tag = count;
[self.view addSubview:followbutn];
}
count++;
}
-(void)followaction:(id)sender
{
UIButton *button = (UIButton *)sender;
NSLog(@"%d",button.tag);
[followbutn viewWithTag:button.tag];
[followbutn setImage:[UIImage imageNamed:@"checkbox_checked.png"] forState:UIControlStateNormal];
}
int count=0;
对于(int i=0;count那么基本上您想要实现一个复选框?您应该创建UIButton
的子类。然后将这行代码添加到UIViewController
或按钮的其他容器中
[myButton addTarget:self action:@selector(self-tapped)for ControlEvents:UIControlEventTouchDown];
然后将以下方法添加到容器中:
-(void)selfTapped{
//CustomButton is the UIButton Subclass
for(CustomButton *but in buttonArray){
if(but.checked){
but.checked = NO;
[but setImage:[UIImage imageNamed:@"unchecked.png"] forControlEvents:UIControlEventTouchDown];
}else{
but.checked = YES
[but setImage:[UIImage imageNamed:@"checked.png"] forControlEvents:UIControlEventTouchDown];
}
}
}
如果您有任何问题,请告诉我。
Checked是一个BOOL
,同样,此方法在UIButton
的子类中,并且此代码中的两个图像是显示与BOOL
值对应的选中和取消选中复选框的图像
编辑:请查看我在上面对您的评论所做的编辑。只要我理解您的问题,我想您希望您的按钮可以作为单选按钮使用。
为此,为每个按钮指定唯一的标记,而不是在这里使用一个计数变量标记。
使用该标记通过更改按钮的图像来检查按钮并删除其他按钮的标记接受已检查的按钮。当您要再次检查其他按钮时,请初始化标记值。在代码中使用该标记。…运行此循环,直到标记值达到最大值
-(void)followaction:(id)sender
{
for (int i = 0; i < value; i++)
{
UIButton *button = (UIButton *)[button viewWithTag:i];
[button setImage:[UIImage imageNamed:@"checkbox_unchecked.png"] forState:UIControlStateNormal];
}
UIButton *button = (UIButton *)sender;
[button viewWithTag:button.tag];
[button setImage:[UIImage imageNamed:@"checkbox_checked.png"] forState:UIControlStateNormal];
}
-(void)followaction:(id)发送者
{
for(int i=0;i
感谢您的即时回复。但点击按钮底部时,选中的图像会突出显示。我需要选中的图像位于按钮上,其他按钮上有未选中的图像。我已经这样做了。这很好。但我只想签入一个按钮,而不是多个选择。感谢aloot Rajneesh,我能够解决此问题你的帮助有问题。