IOS开发教程自定义CheckBox控件.docx_第1页
IOS开发教程自定义CheckBox控件.docx_第2页
IOS开发教程自定义CheckBox控件.docx_第3页
IOS开发教程自定义CheckBox控件.docx_第4页
IOS开发教程自定义CheckBox控件.docx_第5页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

IT在线教育平台麦子学院:首先创建工程文件,CheckBox,然后创建CheckBox类,继承自UIView,同时添加资源文件,选中、取消的图片,添加完成以后目录如下:CheckBox类头文件代码如下:1. #import 2. 3. protocol CheckBoxDelegate;4. 5. interface CheckBox : UIView6. 7. property(nonatomic,retain)NSString *text;/显示文字8. property(nonatomic,assign)BOOL checked;/是否选中9. 10. property(nonatomic,retain) id delegate;/代理11. 12. -(id)initWithText:(NSString *)text frame:(CGRect)frame;/初始化13. 14. end15. 16. protocol CheckBoxDelegate17. 18. -(void)onChangeDelegate:(CheckBox *)checkbox isCheck:(BOOL)isCheck;19. 20. end复制代码CheckBox实现代码如下:1. #import CheckBox.h2. interface CheckBox()3. 4. property(nonatomic,retain)UIImage *onImage;5. property(nonatomic,retain)UIImage *offImage;6. 7. end8. 9. 10. implementation CheckBox11. 12. -(void)dealloc13. _text release;14. _delegate release;15. _onImage release;16. _offImage release;17. super dealloc;18. 19. - (id)initWithFrame:(CGRect)frame20. 21. self = super initWithFrame:frame;22. if (self) 23. / Initialization code24. 25. return self;26. 27. -(id)initWithText:(NSString *)text frame:(CGRect)frame28. self=super initWithFrame:frame;29. if(self)30. _text=text;31. self.backgroundColor=UIColor clearColor;32. self.onImage=UIImage imageNamed:chk_on.png;/选中图片33. self.offImage=UIImage imageNamed:chk_off.png;/取消图片34. 35. return self;36. 37. -(void)setChecked:(BOOL)checked38. _checked=checked;39. /注册代理事件,通知状态改变40. if(self.delegate respondsToSelector:selector(onChangeDelegate:isCheck:)41. self.delegate onChangeDelegate:self isCheck:_checked;42. 43. 44. self setNeedsDisplay;45. 46. -(void)drawRect:(CGRect)rect/将text,image绘制到UIView上面47. UIImage *image=self.checked?self.onImage:self.offImage;48. image drawAtPoint:CGPointMake(5, 8);49. UIFont *font=UIFont systemFontOfSize:16.0f;50. self.text drawAtPoint:CGPointMake(25, 8) withFont:font;51. 52. /点击事件53. -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event54. self.checked=!self.checked;55. 56. 57. 58. end复制代码调用代码如下:首先引入CheckBox,1. #import ViewController.h2. #import CheckBox.h3. 4. interface ViewController ()5. 8. 9. 10. end11. 12. implementation ViewController13. 14. - (void)viewDidLoad15. 16. super viewDidLoad;17. / Do any additional setup after loading the view, typically from a nib.18. CheckBox *cb=CheckBox alloc initWithText:乒乓球 frame:CGRectMake(10, 10, 80, 30);19. cb.delegate=self;/设置委托20. self.view addSubview:cb;21. cb release;22. 23. 24. -(void)onChangeDelegate:(CheckBox *)checkbox isCheck:(BOOL)isCheck/25. NSLog(text:%,State:%,checkbox.text,isCheck?YES:NO);26. 27. 28. - (void)didReceiveMemoryWarning29. 30. super didReceiv

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论