What is the best way to make a UIButton checkbox?

风格不统一 提交于 2019-12-17 23:08:15

问题


I am trying to make a standard check box for my iPhone app from a UIButton with a title and image. The button image changes between an "unchecked" image and a "checked" image.

At first I tried subclassing UIButton but UIButton has no -init*** method to use in my -init method.

What is the best way to do this?

Thanks in advance.


回答1:


You shouldn't need to subclass the UIButton class. By design, Objective-C favors composition over inheritance.

UIButton is a subclass of UIControl, which has a selected property. You can use this property to toggle the on/off behaviour of a checkbox, just the same way a UISwitch does.

You can attach an action to the button's touched up inside event, and perform the toggling in there, something like this:

// when you setup your button, set an image for the selected and normal states
[myCheckBoxButton setImage:checkedImage forState:UIControlStateSelected];
[myCheckBoxButton setImage:nonCheckedImage forState:UIControlStateNormal];

- (void)myCheckboxToggle:(id)sender
{
    myCheckboxButton.selected = !myCheckboxButton.selected; // toggle the selected property, just a simple BOOL
}



回答2:


Set the images in the button:

[button setImage:uncheckedImage forState:UIControlStateNormal]
[button setImage:checkedImage forState:UIControlStateSelected]

Now all you need to do is:

button.selected = state

and the correct images will display.




回答3:


All you need to do is set 2 different images for the states UIControlStateNormal and UIControlStateSelected, then in your selector, changed the selected property of the button.

Here is a working example (replace image names with your own):

- (void)loadView {
    // ...
    UIButton *chkBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [chkBtn setFrame:CGRectMake(0, 0, 300, 25)];

    [chkBtn setImage:[UIImage imageNamed:@"UNCHECKED.png"] 
            forState:UIControlStateNormal];
    [chkBtn setImage:[UIImage imageNamed:@"CHECKED.png"]
            forState:UIControlStateSelected];

    [chkBtn addTarget:self 
               action:@selector(chkBtnHandler:) 
     forControlEvents:UIControlEventTouchUpInside];

    // Optional title change for checked/unchecked
    [chkBtn setTitle:@"I am NOT checked!"
            forState:UIControlStateNormal];
    [chkBtn setTitle:@"I am checked!"
            forState:UIControlStateSelected];

    [self.view addSubview:chkBtn];
    [chkBtn release], chkBtn = nil;
    // ...
}

- (void)chkBtnHandler:(UIButton *)sender {
    // If checked, uncheck and visa versa
    [sender setSelected:!sender isSelected];
}



回答4:


For anyone interested in the future - instead of doing it yourself just download the link below from GitHub and it has it subclassed from UIControl already and functions perfectly as a checkbox. Also includes a sample project on how easy it is to use:

https://github.com/Brayden/UICheckbox




回答5:


I have used M13Checkbox in one of my projects. Works ok.

https://github.com/Marxon13/M13Checkbox




回答6:


Did you try overriding the initWithCoder method, just in case it is loaded from a nib somehow?




回答7:


    UIImage* nonCheckedImage=[UIImage imageNamed:@"ic_check_box_outline_blank_grey600_48dp.png"];//[UIImage init
    UIImage* CheckedImage=[UIImage imageNamed:@"ic_check_box_black_48dp.png"];//[UIImage init

    //ic_check_box_black_48dp.png
    [_checkBox setImage:CheckedImage forState:UIControlStateSelected];
    [_checkBox setImage:nonCheckedImage forState:UIControlStateNormal];
}

- (IBAction)checkBoxToggle:(id)sender {
    _checkBox.selected = !_checkBox.selected; // toggle the selected property, just a simple BOOL

}

the image you can use google icon




回答8:


Try this:-

-(IBAction)clickCheckButton:(UIButton *)sender {

    if (sender.tag==0) {

    sender.tag = 1;

    [sender setImage:[UIImage imageNamed:@"check.png"] forState:UIControlStateNormal]; 

    }else

    {

    sender.tag = 0;

    [sender setImage:[UIImage imageNamed:@"uncheck.png"] forState:UIControlStateNormal]; } } sender.tag = 0;

    [sender setImage:[UIImage imageNamed:@"uncheck.png"] forState:UIControlStateNormal];

    } 
}



回答9:


Why not use a switch - UISwitch? This is used to display an element showing the boolean state of a value.



来源:https://stackoverflow.com/questions/2227366/what-is-the-best-way-to-make-a-uibutton-checkbox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!