iPhone开发进阶(4)编程定制UIButton案例实现

移动开发 iOS
iPhone开发编程定制UIButton案例实现是本文要介绍的内容,主要是来讲述一下自动创建 UIButton 而不使用 XIB 文件,来看具体内容。

iPhone开发编程定制UIButton案例实现是本文要介绍的内容,主要是来讲述一下自动创建 UIButton 而不使用 XIB 文件。通过这一节的学习,我们可以掌握不通过 XIB (InterfaceBuilder) 来使用 UIControl 的 addTarget 方法、对应相应的事件动作。

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

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

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

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

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

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

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

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

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

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

  1. -(void)countup:(id)inSender  

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

而如果函数类型是

  1. -(void)countup  

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

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

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

  1. // ***种处理方法  
  2. -(void)countup:(id)inSender {  
  3.     count++;  
  4.     [inSender setTitle:[NSString  
  5.         stringWithFormat:@"count:%d", count]  
  6.         forState:UIControlStateNormal];  
  7. }  
  8.  
  9. // 第二种处理方法  
  10. -(void)countup {  
  11.     count++;  
  12. }  
  13.  
  14. -(void)countup:(id)inSender forEvent:(UIEvent *)event {  
  15.     count++;  
  16.     [inSender setTitle:[NSString  
  17.         stringWithFormat:@"count:%d", count]  
  18.         forState:UIControlStateNormal];  
  19. }  
  20.  
  21. - (void)viewDidLoad {  
  22.     [super viewDidLoad];  
  23.     self.view.backgroundColor = [UIColor blueColor];  
  24.     UIButton* button = [UIButton buttonWithType:UIButtonTypeInfoLight];  
  25.     button.frame = CGRectMake(100,100,100,100);  
  26.     // 注册***种方法  
  27.     [button addTarget:self action:@selector(countup:)  
  28.         forControlEvents:UIControlEventTouchUpInside];  
  29.     // 注册第二种方法  
  30.     [button addTarget:self action:@selector(countup)  
  31.         forControlEvents:UIControlEventTouchUpInside];  
  32.     [button addTarget:self action:@selector(countup:forEvent:)  
  33.         forControlEvents:UIControlEventTouchUpInside];  
  34.     [self.view addSubview:button];  
  35. }  

编译以后,显示如下:

编程定制UIButton案例实现

小结:iPhone开发编程定制UIButton案例实现的内容介绍完了,希望通过本文的学习能对你有所帮助!如果想继续深入了解的话,请参考以下几篇文章:

iPhone开发进阶(1)iPhone应用程序项目构成案例实现

iPhone开发进阶(2)iPhone应用程序的启动过程

iPhone开发进阶(3)编程定制UIViewController案例实现

责任编辑:zhaolei 来源: 博客园
相关推荐

2011-08-17 16:23:31

iPhone开发UIViewContr

2011-08-17 16:12:20

iPhone应用程序

2011-08-17 16:16:29

iPhone应用程序启动过程

2011-05-03 15:28:15

BlackBerryWidget

2013-12-27 09:54:58

Android开发NDK

2011-08-15 15:44:46

iPhone开发PDF

2011-08-18 16:24:44

iPhone开发图片

2010-12-23 09:11:17

读写Android文件

2012-02-07 10:05:40

jQuery MobijQuery Mobi

2011-08-16 15:48:37

iPhone开发抓图程序

2011-08-19 10:13:05

iPhone开发

2011-08-19 11:10:31

iPhone应用

2011-08-18 15:24:40

iPhone国际化

2021-01-20 08:16:06

异步Dotnet Core多路径

2023-08-01 08:52:03

WebRTC.Net线程

2011-08-19 10:05:30

iPhone开发

2014-01-07 14:53:37

Android开发依赖注入Roboguice

2011-07-29 14:18:46

iPhone开发 动画

2011-10-18 10:25:08

Android应用开发

2011-10-18 10:17:39

Android应用开发
点赞
收藏

51CTO技术栈公众号