开发者

Is that possible to make radiobuttons on scrollview

开发者 https://www.devze.com 2023-03-29 02:44 出处:网络
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 t

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

精彩评论

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

关注公众号