iPhone开发进阶(6)— 编程定制UIButton
2010-04-24 11:54
267 查看
27二/100
博主:易飞扬
原文链接 : http://www.yifeiyang.net/iphone-developer-advanced-6-programming-custom-uibutton/
转载请保留上面文字。
iPhone开发进阶(6)--- 编程定制UIButton
上一回介绍了不使用 XIB 文件来定义 UIViewController 的方法。这一回说一说自动创建 UIButton 而不使用 XIB 文件。
通过这一节的学习,我们可以掌握不通过 XIB (InterfaceBuilder) 来使用 UIControl 的 addTarget 方法、对应相应的事件动作。
具体的例子是基于上一讲中的 CustomViewController 类,按钮按下是计数器加一,并显示在视图上。
首先,在 CustomViewController 类中添加技术用的变量 count。
接下来,添加按钮按下时调用的方法。
setTitle 方法设定 UIButton 的标题。使用 forState: 来指定该标题显示的状态(按下,弹起,通常),这里指定通常状态显示的标题。当然,使用 UIControlStateNormal 也是可以的。
注册按钮按下时的事件函数可以通过 UIControl 类中的 addTarget:action:forControlEvents: 方法(UIButton 继承了UIControl 类,所以可以直接使用)。如下所示:
forControlEvents: 中设定 UIControlEventTouchUpInside 是指在按钮上按下时响应。
因为动作函数(countup)的类型是
则在注册的时候需要写 countup: 。
而如果函数类型是
的话,则是 countup ,这时 addTarget 接收的函数类型如下所示:
同一响应,也可以注册多个处理,比如下面的代码,将上面两种类型的动作函数都注册了:
编译以后,显示如下:
博主:易飞扬
原文链接 : http://www.yifeiyang.net/iphone-developer-advanced-6-programming-custom-uibutton/
转载请保留上面文字。
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 | -(void)countup:(id)inSender { count++; // 计数器自加 // inSender 是被点击的 Button 的实例,下面设置其标题 [inSender setTitle:[NSString stringWithFormat:@"count:%d", count] forState:UIControlStateNormal]; } |
注册按钮按下时的事件函数可以通过 UIControl 类中的 addTarget:action:forControlEvents: 方法(UIButton 继承了UIControl 类,所以可以直接使用)。如下所示:
1 2 3 4 | - (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]; } |
因为动作函数(countup)的类型是
1 | -(void)countup:(id)inSender |
而如果函数类型是
1 | -(void)countup |
1 | - (void) countup:(id)sender forEvent:(UIEvent *)event |
1 2 3 4 | // 第一种处理方法 -(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]; } |
相关文章推荐
- iPhone开发进阶(6)--- 编程定制UIButton
- iPhone开发进阶(6)--- 编程定制UIButton
- iPhone开发进阶(6)--- 编程定制UIButton
- iPhone开发进阶(5) --- 编程定制UIViewController
- iPhone开发进阶(5) — 编程定制UIViewController
- iPhone开发进阶(5) --- 编程定制UIViewController
- iPhone开发进阶(5) --- 编程定制UIViewController
- 水果达人-iPhone游戏开发 iPhone软件外包 iOS软件定制 iOS编程开发 App外包
- iPhone开发Socket编程使用开源代码之AsyncSocket
- 轻松搞定iPhone socket 编程 用代码说话(iphone开发入门 4)
- iPhone开发进阶————利用ModalViewController切换View
- Python进阶开发之元类编程
- ipad应用定制、ipad应用开发、iPhone应用定制、iPhone应用开发、iOS应用定制、iO
- 让不懂编程的人爱上iPhone开发(2017秋iOS11+Swift4+Xcode9版)-第4篇
- 让不懂编程的人爱上iPhone开发(2017iOS11+Swift4+Xcode9版)-第5篇
- Iphone开发UIButton小结
- 让不懂编程的人爱上iPhone开发(2017秋iOS11+Swift4+Xcode9版)-第7篇
- iPhone进阶开发(原稿是一个ppt)
- iPhone开发进阶(10)--- 在程序中使用GPS
- iPhone开发进阶(11)— 多线程的使用与注意事项