IOS开源项目(2)之RadioButton单选控件学习

来源:互联网 发布:js 构造函数怎么写 编辑:程序博客网 时间:2024/06/17 21:59

1 前言

众所周知在IOS中没有单选按钮这一控件,今天我们来学习一下简单的单选控件。类似与Web中的radio表单元素。

2 详述

本控件单纯的利用按钮控件和NSObject的respondsToSelector方法来判断某一个类中是否存在某方法。

代码概述:

ZYRadioButton.h(控件头文件):

#import <UIKit/UIKit.h>@protocol RadioButtonDelegate <NSObject>-(void)radioButtonSelectedAtIndex:(NSUInteger)index inGroup:(NSString*)groupId;@end@interface ZYRadioButton : UIView{    NSString *_groupId;    NSUInteger _index;    UIButton *_button;}//GroupId@property(nonatomic,retain)NSString *groupId;//Group的索引@property(nonatomic,assign)NSUInteger index;//初始化RadioButton控件-(id)initWithGroupId:(NSString*)groupId index:(NSUInteger)index;//为+(void)addObserverForGroupId:(NSString*)groupId observer:(id)observer;@end

ZYViewController.m(视图控制器中的代理方法):

//代理方法-(void)radioButtonSelectedAtIndex:(NSUInteger)index inGroup:(NSString *)groupId{    NSLog(@"changed to %d in %@",index,groupId);}

具体详细代码请见文章最后的代码下载链接。

运行结果:


选中某一选项后结果:


控制台显示结果:

2013-05-22 21:50:46.033 RadioButtonDemo[467:c07] changed to 0 in first group

3 结语

以上是所有内容希望对大家有所帮助。

Demo代码下载:http://download.csdn.net/detail/u010013695/5431201

原创粉丝点击