控件学习---UIButton--摘自培训资料

来源:互联网 发布:淘宝开店协议无法同意 编辑:程序博客网 时间:2024/05/29 13:44


·UIControl 是所有具有事件处理控件的父类
· 控件主要响应3种事件: 基于触摸的事件、基于值的事件、基于编辑的事件。
·常用事件: UIControlEventTouchUpInside 如按钮的点击事件、

· 控件主要响应3种事件:基于触摸的事件、基于值的事件、基于编辑的事件。
·常用事件: UIControlEventTouchUpInside 如按钮的点击事件、

UIControlEventValueChanged 如进度条拖动。

常用方法:

//添加一个事件

- (void)addTarget:(id)target
           action:(SEL)action
 forControlEvents:(UIControlEvents)controlEvents

//删除一个事件

- (void)removeTarget:(id)target
              action:(SEL)action
    forControlEvents:(UIControlEvents)controlEvents

事件处理:



UIButton按钮
UIButton按钮,主要用来响应用户点击事件。
· 常用属性
·buttonType 按钮显示样式类型
·titleLabel 按钮标题文本Label对象。

常用方法
//设置指定状态对应的标题文本
- (void)setTitle:(NSString *)title forState:(UIControlState)state;
//设置指定状态对应的标题颜⾊
- (void)setTitleColor:(UIColor *)color forState:(UIControlState)state;
//设置指定状态对应的显⽰图⽚
- (void)setImage:(UIImage *)image forState:(UIControlState)state;
//设置指定状态对应的背景图⽚
- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state;
//为按钮添加事件
- (void)addTarget:(id)target action:(SEL)action forControlEvents:
(UIControlEvents)controlEvents;

//设置按钮使能/失能,继承自UIControl类的属性
@property(nonatomic, getter=isEnabled) BOOL enabled

UIButton状态
·button有很多种状态,不同的状态可以对应不同的按钮图片
UIButton状态:
UIControlStateNormal // 正常状态
UIControlStateHighlighted // 高亮状态
UIControlStateDisabled // 禁用状态
UIControlStateSelected // 选中状态
UIControlStateApplication //
UIControlStateReserved // 系统保留状态
UIButton 方法:
- (void)setTitle:(NSString *)title forState:(UIControlState)state;
- (void)setTitleColor:(UIColor *)color forState:(UIControlState)state;
- (void)setTitleShadowColor:(UIColor *)color forState:
(UIControlState)state;
- (void)setImage:(UIImage *)image forState:(UIControlState)state;
- (void)setBackgroundImage:(UIImage *)image forState:
(UIControlState)state;


UIButton示例代码

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame =CGRectMake(20,40,100,30);
//设置平常状态下按钮的标题
[button setTitle:@"按钮"forState:UIControlStateNormal];
//设置⾼亮状态下按钮的标题
[button setTitle:@"按钮按下"forState:UIControlStateHighlighted];
//设置点击事件响应的⽅法
[button addTarget:selfaction:@selector(buttonAction)
forControlEvents:UIControlEventTouchUpInside];
//设置平常状态下标题的颜⾊
[button setTitleColor:[UIColorblackColor]forState:UIControlStateNormal];
//设置⾼亮状态下标题的颜⾊
[button setTitleColor:[UIColorredColor]forState:UIControlStateHighlighted];
//设置标题的字体
button.titleLabel.font= [UIFontsystemFontOfSize:14];


0 0
原创粉丝点击