Checkbox in iOS application

前端 未结 13 1454
野趣味
野趣味 2020-12-01 01:48

I need to add checkbox controls to my form. I know that there is no such control in iOS SDK. How could I do this?

13条回答
  •  孤城傲影
    2020-12-01 02:30

    in .h file

    #import 
    
    @interface ViewController : UIViewController
    {
        BOOL isChecked;
        UIImageView * checkBoxIV;
    }
    @end
    

    And .m file

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        isChecked = NO;
    
        //change this property according to your need
        checkBoxIV = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 15, 15)]; 
        checkBoxIV.image =[UIImage imageNamed:@"checkbox_unchecked.png"]; 
    
        checkBoxIV.userInteractionEnabled = YES;
        UITapGestureRecognizer *checkBoxIVTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handlecheckBoxIVTapGestureTap:)];
        checkBoxIVTapGesture.numberOfTapsRequired = 1;
        [checkBoxIV addGestureRecognizer:checkBoxIVTapGesture];
    }
    
    - (void)handlecheckBoxIVTapGestureTap:(UITapGestureRecognizer *)recognizer {
        if (isChecked) {
            isChecked = NO;
            checkBoxIV.image =[UIImage imageNamed:@"checkbox_unchecked.png"];
        }else{
            isChecked = YES;
            checkBoxIV.image =[UIImage imageNamed:@"checkbox_checked.png"];   
        }
    }
    

    This will do the trick...

提交回复
热议问题