是否可以在scrollview上制作单选按钮

时间:2011-08-18 08:07:30

标签: iphone objective-c ios uiscrollview

我想在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;
    }
}

1 个答案:

答案 0 :(得分:0)

对于scrollview,

应为:

for (UIButton *but in [scrollview subviews]) {...

NOT: for (UIButton *but in [self.view subviews]) {...