自定义控件复选框和单选框的实现

我们先实现单个按钮,为了复用,不管单选还是复选按钮都是使用同一个类来实现,为了区别单选还是复选,我们用一个自定义枚举类型CheckButtonStyle属性style来区别,当其值设置为CheckButtonStyleDefault或CheckButtonStyleBox时,为复选按钮:

  当其值设为CheckButtonStyleRadio时,为单选按钮:

当按钮在选中/反选状态间切换时,文字左边的图片自动转换。

整个控件是由一个ImageView、一个Label、一个BOOL变量及其他变量组成,.h文件如下:

typedef enum {

    CheckButtonStyleDefault = 0 ,

    CheckButtonStyleBox = 1 ,

    CheckButtonStyleRadio = 2

} CheckButtonStyle;

#import <Foundation/Foundation.h>

 

@interface CheckButton : UIControl {

//UIControl* control;

UILabel * label ;

UIImageView * icon ;

BOOL checked ;

id value , delegate ;

CheckButtonStyle style ;

NSString * checkname ,* uncheckname ; // 勾选/反选时的图片文件名

}

@property ( retain , nonatomic ) id value,delegate;

@property ( retain , nonatomic )UILabel* label;

@property ( retain , nonatomic )UIImageView* icon;

@property ( assign )CheckButtonStyle style;

-( CheckButtonStyle )style;

-( void )setStyle:( CheckButtonStyle )st;

-( BOOL )isChecked;

-( void )setChecked:( BOOL )b;

@end

具体实现如下:

#import "CheckButton.h"

 

 

@implementation CheckButton

@synthesize label,icon,value,delegate;

-( id )initWithFrame:( CGRect ) frame

{

if ( self =[ super initWithFrame : frame ]) {

icon =[[ UIImageView alloc ] initWithFrame :

  CGRectMake ( 10 , 0 , frame . size . height , frame . size . height )];

[ self setStyle : CheckButtonStyleDefault ]; // 默认风格为方框(多选)样式

//self.backgroundColor=[UIColor grayColor];

[ self addSubview : icon ];

label =[[ UILabel alloc ] initWithFrame : CGRectMake ( icon . frame . size . width + 24 , 0 ,

   frame . size . width - icon . frame . size . width - 24 ,

   frame . size . height )];

label . backgroundColor =[ UIColor clearColor ];

label . font =[ UIFont fontWithName : @"Arial" size : 20 ];

label . textColor =[ UIColor

  colorWithRed : 0xf9 / 255.0

  green : 0xd8 / 255.0

  blue : 0x67 / 255.0

  alpha : 1 ];

label . textAlignment = UITextAlignmentLeft ;

[ self addSubview : label ];

[ self addTarget : self action : @selector ( clicked ) forControlEvents : UIControlEventTouchUpInside ];

}

return self ;

}

-( CheckButtonStyle )style{

return style ;

}

-( void )setStyle:( CheckButtonStyle )st{

style =st;

switch ( style ) {

case CheckButtonStyleDefault :

case CheckButtonStyleBox :

checkname = @"checked.png" ;

uncheckname = @"unchecked.png" ;

break ;

case CheckButtonStyleRadio :

checkname = @"radio.png" ;

uncheckname = @"unradio.png" ;

break ;

default :

break ;

}

[ self setChecked : checked ];

}

-( BOOL )isChecked{

return checked ;

}

-( void )setChecked:( BOOL )b{

if (b!= checked ){

checked =b;

}

if ( checked ) {

[ icon setImage :[ UIImage imageNamed : checkname ]];

} else {

[ icon setImage :[ UIImage imageNamed : uncheckname ]];

}

}

-( void )clicked{

[ self setChecked :! checked ];

if ( delegate != nil ) {

SEL sel= NSSelectorFromString ( @"checkButtonClicked" );

if ([ delegate respondsToSelector :sel]){

[ delegate performSelector :sel];

}

}

-( void )dealloc{

value = nil ; delegate = nil ;

[ label release ];

[ icon release ];

[ super dealloc ];

}

@end

使用CheckButton类很简单,构造、设置标签文本等属性,然后addSubview:

CheckButton * cb=[[ CheckButton a lloc ] initWithFrame : CGRectMake ( 20 , 60 , 260 , 32 )];

cb. label . text = @"checkbutton1" ;

cb. value =[[ NSNumber alloc ] initWithInt : 18 ];

cb. style = CheckButtonStyleDefault ;

[ self . view addSubview :cb];

二、单选按钮组的实现

复选按钮无所谓“组”的概念,单选按钮则不同。在同一个组中,单选按钮只允许同时选择一个按钮,不能选多个,因此我们要实现一个单选按钮组的类:

#import <Foundation/Foundation.h>

#import "CheckButton.h"

 

@interface RadioGroup : NSObject {

NSMutableArray * children ;

NSString * text ;

id value ;

}

@property ( readonly )NSString* text;

@property ( readonly ) id value;

-( void )add:( CheckButton *)cb;

-( void )checkButtonClicked:( id )sender;

@end

#import "RadioGroup.h"

 

 

@implementation RadioGroup

@synthesize text,value;

-( id )init{

if ( self =[ super init ]){

children =[[ NSMutableArray alloc ] init ];

}

return self ;

}

-( void )add:( CheckButton *)cb{

cb. delegate = self ;

if (cb. checked ) {

text =cb. label . text ;

value =cb. value ;

}

[ children addObject :cb];

}

-( void )checkButtonClicked:( id )sender{

CheckButton * cb=( CheckButton *)sender;

if (!cb. checked ) {

// 实现单选

for ( CheckButton * each in children ){

if (each. checked ) {

[each setChecked : NO ];

}

}

[cb setChecked : YES ];

// 复制选择的项

text =cb. label . text ;

value =cb. value ;

}

NSLog ( @"text:%@,value:%d" , text ,[( NSNumber *) value intValue ]);

}

-( void )dealloc{

[ text release ];

value = nil ;

[ children release ];

[ super dealloc ];

}

@end

单选按钮组在ViewController中的使用:

-( id )initWithNibName:( NSString *)nibNameOrNil bundle:( NSBundle *)nibBundleOrNil{

if ( self =[ super initWithNibName :nibNameOrNil bundle :nibBundleOrNil]){

// 单选按钮组

rg =[[ RadioGroup alloc ] init ];

// 1 个单选按钮

CheckButton * cb=[[ CheckButton alloc ] initWithFrame : CGRectMake ( 20 , 60 , 260 , 32 )];

// 把单选按钮加入按钮组

[ rg add :cb];

cb. label . text = @"★" ;

cb. value =[[ NSNumber alloc ] initWithInt : 1 ];

// 把按钮设置为单选按钮样式

cb. style = CheckButtonStyleRadio ;

// 加入视图

[ self . view addSubview :cb];

[cb release ]; //add 后,会自动持有,可以释放

// 2 个单选按钮

cb=[[ CheckButton alloc ] initWithFrame : CGRectMake ( 20 , 100 , 260 , 32 )];

[ rg add :cb];

cb. label . text = @"★★" ;

cb. value =[[ NSNumber alloc ] initWithInt : 2 ];

cb. style = CheckButtonStyleRadio ;

[ self . view addSubview :cb];

[cb release ];

// 3 个单选按钮

cb=[[ CheckButton alloc ] initWithFrame : CGRectMake ( 20 , 140 , 260 , 32 )];

// 各种属性必须在 [rg addv] 之前设置,否则 text value 不会被 populate

cb. checked = YES ;

cb. label . text = @"★★★" ;

cb. value =[[ NSNumber alloc ] initWithInt : 3 ];

cb. style = CheckButtonStyleRadio ;

[ self . view addSubview :cb];

[ rg add :cb]; // 属性设置完之后再 add

[cb release ];

// 4 个单选按钮

cb=[[ CheckButton alloc ] initWithFrame : CGRectMake ( 20 , 180 , 260 , 32 )];

[ rg add :cb];

cb. label . text = @"★★★★" ;

cb. value =[[ NSNumber alloc ] initWithInt : 4 ];

cb. style = CheckButtonStyleRadio ;

[ self . view addSubview :cb];

[cb release ];

// 5 个单选按钮

cb=[[ CheckButton alloc ] initWithFrame : CGRectMake ( 20 , 220 , 260 , 32 )];

[ rg add :cb];

cb. label . text = @"★★★★★" ;

cb. value =[[ NSNumber alloc ] initWithInt : 5 ];

cb. style = CheckButtonStyleRadio ;

[ self . view addSubview :cb];

[cb release ];

}

return self ;

}

运行效果:


 

 

转载于:https://www.cnblogs.com/encounter/archive/2011/01/18/2188520.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/298731.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

单文件组件的组件传值_移动端组件化架构(下)

我的组件化方案对于项目架构来说&#xff0c;一定要建立于业务之上来设计架构。不同的项目业务不同&#xff0c;组件化方案的设计也会不同&#xff0c;应该设计最适合公司业务的架构。架构设计以我之前公司项目为例&#xff0c;项目是一个地图导航应用&#xff0c;业务层之下的…

linux运维趋势 37期刊为啥没有,linux运维常见问题

1、systemd查看日志文件有隐藏该如何处理&#xff1f;systemd统一管理所有Unit的启动日志&#xff0c;包含内核日志和应用日志。在默认情况下&#xff0c;systemd日志保存于/run/log/journal中&#xff0c;系统重启后会清除&#xff0c;这里面的日志文件是二进制形式保存的&…

2010年下半年计算机专业技术资格考试工作安排

<?xml:namespace prefix o ns "urn:schemas-microsoft-com:office:office" />2010年下半年计算机专业技术资格考试工作安排考试日期: 2010年11月13、14日级别<?xml:namespace prefix st1 ns "urn:schemas-microsoft-com:office:smarttags" …

为什么圆是360度?超颠覆的解释

圆为什么有360度&#xff1f;为什么不是300度呢&#xff1f;古文明时期人类把很多不能解释的自然现象归结为“天意”真的有天意吗&#xff1f;我们把圆分成等份&#xff0c;奇迹出现了.....依次等分下去&#xff0c;结果一样...任何被分成等分的角度的所有数字之和为9现在我们来…

我获得“微软MVP”奖项,后续将会贡献更多技术内容

昨天晚上&#xff0c;我收到了微软总部发来的“恭喜获得MVP”的邮件。请点击【阅读原文】查看我的MVP Profile页面。有的朋友说“一直以为你早就是MVP了”。其实这么多年我做的技术贡献主要是录编程视频教程&#xff0c;而这些视频教程都是通过BT下载等方式传播&#xff0c;没有…

[Spring MVC] - InitBinder验证

Spring MVC使用InitBinder验证&#xff1a; 使用InitBinder做验证的情况一般会在此Controller中提交的数据需要有一些是业务性质的&#xff0c;也即比较复杂的验证情况下才会使用。大部份简单的表单验证&#xff0c;使用annotation验证即可以解决。 Annotation验证使用方法可参…

linux6.5进入救援模式,rhel6.5救援模式修复系统

如果系统中很多重要的部分被删除了例如/boot下的所有东西&#xff0c;则可以通过救援模式[rootdazzle1 桌面]# mkdir /backup[rootdazzle1 桌面]# cp /etc/fstab /backup/fstab  //先备份以下fstab文件&#xff0c;也可以不备份自己写[rootdazzle1 桌面]# rm -rf /boot/*  …

C#委托,事件理解入门 (译稿)

http://www.cnblogs.com/finesite/articles/255884.html转载于:https://www.cnblogs.com/lhuser/articles/1941136.html

话里话外:流程图绘制初级:六大常见错误

无论初学者还是老手&#xff0c;在绘制流程图的过程中都不可避免地出现这样那样的错误&#xff0c;我们总结了一下&#xff0c;在流程的绘制过程中&#xff0c;比较容易犯得错误有&#xff1a;1、没使用模板或没正确使用模板。在流程管理项目中&#xff0c;流程梳理之前需要事先…

一名毕业生的自述:我知道我必须写论文,但没聪明到可以写出来......

全世界只有3.14 % 的人关注了爆炸吧知识2020年转眼就到了4月。在即将毕业的学子之间&#xff0c;每天的狂野问候语是这样的&#xff1a;“你论文改完了吗&#xff1f;”“你论文查重率是多少&#xff1f;”“你什么时候答辩&#xff1f;”在微博上实时搜索“翟天临”三个字&…

不是架构的架构之四:业务层的实现与自动代理

我们在开篇中提到&#xff0c;希望能有一种办法&#xff0c;能自动适应系统的环境配置&#xff0c;在局域网小型应用中将直接访问数据库以获得最高的性能&#xff0c;在分布式环境中自动使用WCF来获得较好的安全性和连通性。 但是&#xff0c;我们不希望这样的特性使我们的开发…

每次跳槽,总得面对这摊事

大家好&#xff0c;我是 Z 哥。所谓“跳槽爽&#xff0c;一直跳槽一直爽”。但是&#xff0c;世上哪有那么好的事哦&#xff0c;跳槽虽然可以带来更快的涨薪机会&#xff0c;但是你也是要面对和克服一些新挑战的&#xff0c;其中最大的挑战莫过于要熟悉一个陌生的项目。毕竟&am…

automake

./configure --prefix"/root/code/install/x86" CPPFLAGS"-I/root/src/include" LDFLAGS"-L/usr/local/lib -L/root/code/x86/sqlite" 动态链接 AUTOMAKE_OPTIONSforeign SUBDIRSface cgi bin_PROGRAMSmain main_SOURCESmain.cpp main_LDADDfa…

python程序设计实践教程陈东_Python

“我们正步入一个数据或许比软件更重要的新时代。——Tim OReilly” 运用数据是精准刻画事物、呈现发展规律的主要手段&#xff0c;分析数据展示规律&#xff0c;把思想变得更精细&#xff01; 本课程面向各类编程学习者&#xff0c;讲解利用Python语言表达N维数据并结合数据特…

Silverlight中开发和设计人员的合作文档信息

-----------------------------------------------------------------------------------> copyright:http://www.docin.com/p-34191215.html转载于:https://www.cnblogs.com/molin/archive/2009/12/08/silverlight_manager.html

和男朋友一块儿吃VS单独一人在家吃饭

1 和男朋友一块儿吃VS单独一人在家吃饭2 忍不住要为这位跳高选手鼓掌了3 我们家的蔬菜就没有这种觉悟4 这螳螂拳算是练到家了5 现实中的你胖的一批 6 这套户型咋样&#xff1f;7 你能看出几个字你点的每个赞&#xff0c;我都认真当成了喜欢

MYSQL性能优化分享(分库分表)

1、分库分表 很明显&#xff0c;一个主表&#xff08;也就是很重要的表&#xff0c;例如用户表&#xff09;无限制的增长势必严重影响性能&#xff0c;分库与分表是一个很不错的解决途径&#xff0c;也就是性能优化途径&#xff0c;现在的案例是我们有一个1000多万条记录的用户…

指针04 - 零基础入门学习C语言44

第八章&#xff1a;指针04 让编程改变世界 Change the world by program 小结 归纳起来, 如果有一个实参数组, 想在函数中改变此数组中的元素的值, 实参与形参的对应关系有以下&#xff14;种情况&#xff1a; (1) 形参和实参都用数组名, 如&#xff1a; [codesyntax lang&…

填坑 | .NET 5在Docker中访问MSSQL报错

【.NET Core】| 作者 / Edison Zhou不知道你有没有在.NET Core/.NET 5的Docker访问MS SQL Server数据库&#xff0c;如果有&#xff0c;那么很有可能会遇到这个错误。1SSL版本错误最近在公司用.NET 5重构部分业务服务&#xff0c;由于之前老系统使用了MS SQL Server数据库&…