我想在UIScrollView
上制作单选按钮。当我尝试在UIView
上创建它然后它完美无缺,但当我尝试UIScrollView
时,相同的代码将作为复选框。那么有没有办法在UIScrollView
上制作单选按钮?
我正在使用此代码进行scrollview
for (int i = 0; i < 5; i++) {
UIButton *but = [UIButton buttonWithType:UIButtonTypeCustom];
[but setImage:[UIImage imageNamed:@"emptycheckbox.png"] forState:UIControlStateNormal];
[but setImage:[UIImage imageNamed:@"checkbox.png"] forState:UIControlStateSelected];
[but setFrame:CGRectMake(0, 0, 40, 33)];
[but setCenter:CGPointMake( 50, i*40+20 )];
[but addTarget:self action:@selector(checkboxButton:) forControlEvents:UIControlEventTouchUpInside];
[scrollView addSubview:but];
scrollView.showsVerticalScrollIndicator = NO;
[scrollView setContentSize:CGSizeMake(200,1000)];
}
- (IBAction)checkboxButton:(UIButton *)button{
for (UIButton *but in [self.view subviews]) {
if ([but isKindOfClass:[UIButton class]] && ![but isEqual:button]) {
[but setSelected:NO];
}
}
if (!button.selected) {
button.selected = !button.selected;
}
}
我使用此代码UIView
for (int i = 0; i < 5; i++) {
UIButton *but = [UIButton buttonWithType:UIButtonTypeCustom];
[but setImage:[UIImage imageNamed:@"emptycheckbox.png"] forState:UIControlStateNormal];
[but setImage:[UIImage imageNamed:@"checkbox.png"] forState:UIControlStateSelected];
[but setFrame:CGRectMake(0, 0, 40, 33)];
[but setCenter:CGPointMake( 50, i*40+20 )];
[but addTarget:self action:@selector(checkboxButton:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:but];
}
- (IBAction)checkboxButton:(UIButton *)button{
for (UIButton *but in [self.view subviews]) {
if ([but isKindOfClass:[UIButton class]] && ![but isEqual:button]) {
[but setSelected:NO];
}
}
if (!button.selected) {
button.selected = !button.selected;
}
}
答案 0 :(得分:0)
应为:
for (UIButton *but in [scrollview subviews]) {...
NOT: for (UIButton *but in [self.view subviews]) {...