Checkbox in iOS application

前端 未结 13 1443
野趣味
野趣味 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条回答
  •  萌比男神i
    2020-12-01 02:53

    Extending to Adrean's idea, i've achieved this using a very simple approach.
    My idea is to change button (lets say checkBtn) text depending upon its state, and then change button's state in its IBAction.
    Below is the code how i did this:

    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        [checkBtn setTitle:@"\u2610" forState:UIControlStateNormal];    // uncheck the button in normal state
        [checkBtn setTitle:@"\u2611" forState:UIControlStateSelected];  // check the button in selected state
    }
    
    - (IBAction)checkButtonTapped:(UIButton*)sender {
        sender.selected = !sender.selected;    // toggle button's selected state  
    
        if (sender.state == UIControlStateSelected) {    
            // do something when button is checked 
        } else {
            // do something when button is unchecked
        }
    }
    

提交回复
热议问题