现在的位置: 首页 > 综合 > 正文

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

2013年02月07日 ⁄ 综合 ⁄ 共 983字 ⁄ 字号 评论关闭

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

抱歉!评论已关闭.