您的位置:首页 > 产品设计 > UI/UE

转: IOS ----UIButton用法详解

2014-03-20 17:24 411 查看
这段代码动态的创建了一个UIButton,并且把相关常用的属性都列举了.希望对大家有用.

//这里创建一个圆角矩形的按钮

UIButton *button1 = [UIButton
buttonWithType:UIButtonTypeRoundedRect];

// 能够定义的button类型有以下6种,

// typedef enum {

// UIButtonTypeCustom = 0, 自定义风格

// UIButtonTypeRoundedRect, 圆角矩形

// UIButtonTypeDetailDisclosure, 蓝色小箭头按钮,主要做详细说明用

// UIButtonTypeInfoLight, 亮色感叹号

// UIButtonTypeInfoDark, 暗色感叹号

// UIButtonTypeContactAdd, 十字加号按钮

// } UIButtonType;

//给定button在view上的位置

button1.frame = CGRectMake(20, 20, 280, 40);

//button背景色

button1.backgroundColor = [UIColor clearColor];

//设置button填充图片

//[button1 setImage:[UIImage imageNamed:@"btng.png"]
forState:UIControlStateNormal];

//设置button标题

[button1 setTitle:@"点击" forState:UIControlStateNormal];

//以下是几种状态

// enum {

// UIControlStateNormal = 0, 常规状态显现

// UIControlStateHighlighted = 1 <<
0, 高亮状态显现

// UIControlStateDisabled = 1 << 1,
禁用的状态才会显现

// UIControlStateSelected = 1 << 2,
选中状态

// UIControlStateApplication = 0x00FF0000, 当应用程序标志时

// UIControlStateReserved = 0xFF000000 为内部框架预留,可以不管他

// };

button1.adjustsImageWhenHighlighted = NO;

button1.adjustsImageWhenDisabled = NO;

button1.showsTouchWhenHighlighted = YES;

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

//显示控件

[self.view addSubview:button1];


注意:

[button1 addTarget:self

action:@selector(alarmTimeDone:)

forControlEvents:UIControlEventTouchUpInside

];

addTarget:self 是链接到self,一般都这样设置

action:@selector(alarmTimeDone:) 时间处理函数

forControlEvents:UIControlEventTouchUpInside
控件事件处理的消息


//取消按钮已经添加的所有事件:(这个比较重要,若添加了两个事件
两个事件都会被触发)

[btn removeTarget:nil action:nil
forControlEvents:UIControlEventTouchUpInside];

何时释放release
UIButton?


是否在dealloc中对UIButton对象进行release操作,取决于UIButton初始化的方式。

如果使用
[UIButtonbuttonWithType:UIButtonTypeRoundedRect]这种方式,是不需要进行release操作的,因为
这种方式是自动释放的。如果使用 [[UIButton
alloc]init]的方式,则需要主动进行release释放操作。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: