iPhone开发进阶(6)--- 编程定制UIButton

上一回介绍了不使用 XIB 文件来定义 UIViewController 的方法。这一回说一说自动创建 UIButton 而不使用 XIB 文件。

通过这一节的学习,我们可以掌握不通过 XIB (InterfaceBuilder) 来使用 UIControl 的 addTarget 方法、对应相应的事件动作。

具体的例子是基于上一讲中的 CustomViewController 类,按钮按下是计数器加一,并显示在视图上。

首先,在 CustomViewController 类中添加技术用的变量 count。

1
2
3
4
@interface CustomViewController : UIViewController {
    int count;  // 计数器变量。
}
@end

接下来,添加按钮按下时调用的方法。

1
2
3
4
5
6
7
-(void)countup:(id)inSender {
    count++;                        //  计数器自加
    //  inSender 是被点击的 Button 的实例,下面设置其标题
    [inSender setTitle:[NSString
        stringWithFormat:@"count:%d", count]
        forState:UIControlStateNormal];
}

setTitle 方法设定 UIButton 的标题。使用 forState: 来指定该标题显示的状态(按下,弹起,通常),这里指定通常状态显示的标题。当然,使用 UIControlStateNormal 也是可以的。

注册按钮按下时的事件函数可以通过 UIControl 类中的 addTarget:action:forControlEvents: 方法(UIButton 继承了UIControl 类,所以可以直接使用)。如下所示:

1
2
3
4
5
6
7
8
9
10
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor blueColor];
    UIButton* button = [UIButton buttonWithType:UIButtonTypeInfoLight];
    button.frame = CGRectMake(100,100,100,100);
    // 注册按钮按下时的处理函数
    [button addTarget:self action:@selector(countup:)
        forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
}

forControlEvents: 中设定 UIControlEventTouchUpInside 是指在按钮上按下时响应。

因为动作函数(countup)的类型是

1
-(void)countup:(id)inSender

则在注册的时候需要写 countup:

而如果函数类型是

1
-(void)countup

的话,则是 countup ,这时 addTarget 接收的函数类型如下所示:

1
- (void) countup:(id)sender forEvent:(UIEvent *)event

同一响应,也可以注册多个处理,比如下面的代码,将上面两种类型的动作函数都注册了:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// 第一种处理方法
-(void)countup:(id)inSender {
    count++;
    [inSender setTitle:[NSString
        stringWithFormat:@"count:%d", count]
        forState:UIControlStateNormal];
}

// 第二种处理方法
-(void)countup {
    count++;
}

-(void)countup:(id)inSender forEvent:(UIEvent *)event {
    count++;
    [inSender setTitle:[NSString
        stringWithFormat:@"count:%d", count]
        forState:UIControlStateNormal];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor blueColor];
    UIButton* button = [UIButton buttonWithType:UIButtonTypeInfoLight];
    button.frame = CGRectMake(100,100,100,100);
    // 注册第一种方法
    [button addTarget:self action:@selector(countup:)
        forControlEvents:UIControlEventTouchUpInside];
    // 注册第二种方法
    [button addTarget:self action:@selector(countup)
        forControlEvents:UIControlEventTouchUpInside];
    [button addTarget:self action:@selector(countup:forEvent:)
        forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
}

编译以后,显示如下:

CustomViewController

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值