============================================================
博文原创,转载请声明出处
============================================================
IOS中没有checkbox和radio button ,这一点对于做过android的童鞋会觉得挺无语的,没关系,我们自己写一个view来实现自己的单、复选框。
首先在你的资源里面添加2张图片,一张为按钮的选中状态,另一个为没选中状态,分别命名:unchecked.png,checked.png,下面上代码,相信你看得懂。,如有疑问,欢迎留言交流。
UICheckBox.h
#import <UIKit/UIKit.h>
@interface UICheckBox : UIButton
@end
UICheckBox.m
#import "UICheckBox.h"
@implementation UICheckBox
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setBackGroundImage];
}
return self;
}
- (void)setBackGroundImage {
[self setBackgroundImage:[UIImage imageNamed:@"unchecked.png"] forState:UIControlStateNormal];
[self setBackgroundImage:[UIImage imageNamed:@"checked.png"] forState:UIControlStateSelected];
[self addTarget:self action:@selector(touchUIInside:) forControlEvents:UIControlEventTouchUpInside];
}
-(id)initWithCoder:(NSCoder *)aDecoder{
self = [super initWithCoder:aDecoder];
if (self) {
[self setBackGroundImage];
}
return self;
}
-(IBAction)touchUIInside:(id)sender{
UICheckBox* cb= (UICheckBox*)sender;
cb.selected = !cb.selected;
}