Checkbox in iOS application

前端 未结 13 1375
野趣味
野趣味 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:35

    Subclass UIButton, drop a button to view controller, select it and change class name to CheckBox in the identity inspector.

    #import "CheckBox.h"
    
    @implementation CheckBox
    
    #define checked_icon @"checked_box_icon.png"
    #define empty_icon @"empty_box_icon.png"
    
    - (id)initWithCoder:(NSCoder *)aDecoder
    {
        self = [super initWithCoder:aDecoder];
        if (self)
        {
            [self setImage:[UIImage imageNamed:empty_icon] forState:UIControlStateNormal];
            [self addTarget:self action:@selector(didTouchButton) forControlEvents:UIControlEventTouchUpInside];
        }
        return self;
    }
    
    - (void)didTouchButton {
        selected = !selected;
        if (selected)
            [self setImage:[UIImage imageNamed:checked_icon] forState:UIControlStateNormal];
        else
            [self setImage:[UIImage imageNamed:empty_icon] forState:UIControlStateNormal];
    }
    
    @end
    
    0 讨论(0)
  • 2020-12-01 02:39

    I made one recently. Free to acquire from GitHub. See if this will help. The effect is like

    0 讨论(0)
  • 2020-12-01 02:41

    I made it with a UITextField to avoid drawing anything strange but I liked putting inside as text the tick unicode (Unicode Character 'CHECK MARK' (U+2713)) for the NSString: @"\u2713".

    This way, in my .h file (implementing the protocol for the UITextField 'UITextFieldDelegate'):

    UITextField * myCheckBox;
    

    In my viewDidLoad or the function to prepare the UI:

    ...
    myCheckBox = [[UITextField alloc] initWithFrame:aFrame];
    myCheckBox.borderStyle = UITextBorderStyleRoundedRect; // System look like
    myCheckBox.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    myCheckBox.textAlignment = NSTextAlignmentLeft;
    myCheckBox.delegate = self;
    myCheckBox.text = @" -"; // Initial text of the checkbox... editable!
    ...
    

    Then, add an event selector for reating in the touch event and calling 'responseSelected' event:

    ...
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(checkboxSelected)];
    [myCheckBox addGestureRecognizer:tapGesture];
    ...
    

    Finally respond to that selector

    -(void) checkboxSelected
    {
        if ([self isChecked])
        {
            // Uncheck the selection
            myCheckBox.text = @" -";
        }else{
           //Check the selection
           myCheckBox.text = @"\u2713";
        }
    }
    

    The function 'isChecked' only checks if the text is the @"\u2713" check mark. To prevent showing the keyboard when the text field is selected use the event of the UITextField 'textFieldShouldBeginEditing' and add the event selector to manage the selection:

    -(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
    {
        // Question selected form the checkbox
        [self checkboxSelected];
    
        // Hide both keyboard and blinking cursor.
        return NO;
    }
    
    0 讨论(0)
  • 2020-12-01 02:52

    user Aruna Lakmal; FYI, when you add this code to IB as you describe initWithFrame is not called, initWithCoder is. Implement initWithCoder and it will work as you describe.

    0 讨论(0)
  • 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
        }
    }
    
    0 讨论(0)
  • 2020-12-01 02:54

    this has been driving me mad too and I found a different solution that works well for me and avoids having to use images.

    1. Add a new label object to Interface Builder.
    2. Create an IBOutlet property in Xcode and connect it up to it. In the code below I've called it 'fullyPaid' as I want to know if someone has fully paid a sum of money.
    3. Add the 2 functions below. The 'touchesBegan' function checks if you touched somewhere inside the 'fullyPaid' label object and if so, it calls the 'togglePaidStatus' function. The 'togglePaidStatus' function sets up two strings which have the unicode characters representing an empty box (\u2610) and a checked box (\u2611) respectively. Then it compares what's currently in the 'fullyPaid' object and toggles it with the other string.

    You might want to call the togglePaidStatus function in the viewDidLoad function to set it to an empty string initially.

    Obviously you can add extra checks to prevent users toggling the checkbox if the label is not enabled, but that's not shown below.

    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
    UITouch *touch = [touches anyObject];   
        if (CGRectContainsPoint([fullyPaid frame], [touch locationInView:self.view]))
        {
            [self togglePaidStatus];
        }
    }
    -(void) togglePaidStatus
    {
        NSString *untickedBoxStr = [[NSString alloc] initWithString:@"\u2610"];
        NSString *tickedBoxStr = [[NSString alloc] initWithString:@"\u2611"];   
    
        if ([fullyPaid.text isEqualToString:tickedBoxStr])
        {
            fullyPaid.text = untickedBoxStr;
        }
        else
        {
            fullyPaid.text = tickedBoxStr;
        }
    
        [tickedBoxStr release];
        [untickedBoxStr release];
    }
    
    0 讨论(0)
提交回复
热议问题