当前位置:编程学习 > wap >>

IOS自定义view------checkbox .

IOS中没有checkbox和radio button ,这一点对于做过android的童鞋会觉得挺无语的,没关系,我们自己写一个view来实现自己的单、复选框。
首先在你的资源里面添加2张图片,一张为按钮的选中状态,另一个为没选中状态,分别命名:unchecked.png,checked.png,下面上代码,相信你看得懂。,如有疑问,欢迎留言交流。
UICheckBox.h

[cpp]
01.#import <UIKit/UIKit.h>  
02. 
03.@inte易做图ce UICheckBox : UIButton 
04. 
05.@end 
#import <UIKit/UIKit.h>

@inte易做图ce 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.} 

补充:移动开发 , IOS ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,