============================================================博文原創,轉載請聲明出處電子咖啡(原id藍岩)============================================================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; }