明天笔者几篇文章介绍了改控件代码的文章. 关联文章的地址
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
文章结束给大家分享下程序员的一些笑话语录:
PC软件体积大,是因为一个PC软件功能往往较多,能够满足你一个方面的需求,而一个iphone软件往往没几行代码,干一件很小的事情,自然需要的软件就多。就像吃西瓜和吃瓜子的来比数目,单位不同啊。
--------------------------------- 原创文章 By 控件和代码 ---------------------------------