开发者

Is that possible to make radiobuttons on scrollview

I want to make radio buttons on UIScrollView. When I try to make it on UIView then it works perfectly but when i tried on UIScrollView then the same code will work as checkboxes. So is there any way to make radio buttons on UIScrollView?

I am using this code for 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;
    }
}

I an using this code for 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:UIControlStateSele开发者_运维技巧cted];
        [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;
    }
}


for scrollview should be:

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

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜