IOS custom view ------ checkbox.

Source: Internet
Author: User

There is no checkbox or radio button in IOS, which is quite speechless for android kids shoes. It doesn't matter. We can write a view to implement our own tickets and check boxes.
First, add two images to your resource. One is the selected status of the button, and the other is the unselected status, named unchecked.pngand checked.png respectively. The following code is displayed. I believe you can understand it ., If you have any questions, please leave a message.
UICheckBox. h

[Cpp]
01. # import <UIKit/UIKit. h>
02.
03. @ interface UICheckBox: UIButton
04.
05. @ end
# Import <UIKit/UIKit. h>

@ Interface UICheckBox: UIButton

@ EndUICheckBox. m


[Cpp]
01. # import "UICheckBox. h"
02.
03. @ implementation UICheckBox
04.
05.-(id) initWithFrame :( CGRect) frame
06 .{
07. self = [super initWithFrame: frame];
08. if (self ){
09. [self setBackGroundImage];
10 .}
11. return self;
12 .}
13.-(void) setBackGroundImage {
14. [self setBackgroundImage: [UIImage imageNamed: @ "unchecked.png"] forState: UIControlStateNormal];
15. [self setBackgroundImage: [UIImage imageNamed: @ "checked.png"] forState: UIControlStateSelected];
16. [self addTarget: self action: @ selector (touchUIInside :) forControlEvents: UIControlEventTouchUpInside];
17 .}
18.
19.-(id) initWithCoder :( NSCoder *) aDecoder {
20. self = [super initWithCoder: aDecoder];
21. if (self ){
22. [self setBackGroundImage];
23 .}
24. return self;
25 .}
26.
27.-(IBAction) touchUIInside :( id) sender {
28. UICheckBox * cb = (UICheckBox *) sender;
29. cb. selected =! Cb. selected;
30.
31 .}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.