iOS 对于Target-Action设计模式的理解
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了iOS 对于Target-Action设计模式的理解,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3626字,纯文字阅读大概需要6分钟。
内容图文
iOS 开发之 Target-action 模式 Target-action :目标-动作模式,它贯穿于 iOS 开发始终。但是对于初学者来说,还是被这种模
式搞得一头雾水。
其实 Target-action 模式很简单,就是当某个事件发生时,调用那个对象中的那个方法。如:按下按钮时,调用 Controller 里边的 click 方 法。 “ 那个对象 ” 就是 Target , “ 那个方法 ” 就是 Action ,及 Controller 是 Targer , click 方法是 action 。
一般 Target 都是 Controller ,而 Action 有它自己固有的格式: -(IBAction)click:(id)sender 。如下图所示, target 是处理交互事件的对象实例, action 是 target 对象中处理该事件的方法。
这里有两种方式给 “ 炒菜 ” 按钮设置 Action : 1 、直接拖拽连线
2
、以代码的方式实现
在
iOS
中有一个
UIControl
类,该类中定义了一个
-( void )addTarget:( id )target action:(SEL)forControlEvents:(UIControlEvents)controlEvents
方法,大部分视图类都继承自 UIControl 类,所以 " 炒菜 " 按钮可以使用该方法实现 Target-action 模式。在 iOS 中这种设计模式被称作一个对象给另外一个对象发送消息。
- ( void )viewDidLoad{ [super viewDidLoad]; // 给炒菜按钮添加点击事件 // 使用 Target-action 设计模式,在两个对象间直接发送消息 [self.btnCooking addTarget:self action:@selector(pressCooking:)forControlEvents:UIControlEventTouchUpInside];}
1 、 self 指目标对象为当前对象,及 WViewController ;
2 、 action 即 在目标对象上的点击方法;
3 、何时调用该方法, UIControlEventTouchUpInside 即单击时。
“ 炒菜 ” 按钮是一个可交互的视图控件,点击它后,它指定了一个 target (目标对象),并执行目标对象上指定的 action (方法)。
action 方法有以下几种形式:
- ( void )doSomething; // OR - ( void )doSomething:( id )sender; // OR -(IBAction)doSomething:( id )sender; // OR -(IBAction)doSomething:(UIButton *) sender;
这里的 sender ,发送者,就是对 “ 菜单 ” 按钮对象的引用。以下代码是完全用代码定义的一个 UIButton ,并添加在 self.view 中:
- ( void )viewDidLoad{ [super viewDidLoad]; // 实例化按钮,并设置按钮类型为圆角 UIButton *btnCustom = [UIButtonbuttonWithType:UIButtonTypeRoundedRect]; // 设置按钮大小 btnCustom.frame = CGRectMake( 124 , 140 , 73 , 44 ); // 设置按钮标题 [btnCustom setTitle: @" 点击我 ..." forState:UIControlStateNormal]; // 设置按钮点击事件 [btnCustom addTarget:selfaction:@selector(customButton)forControlEvents:UIControlEventTouchUpInside]; // 将按钮添加到 View [self.view addSubview:btnCustom];} /** 自定义按钮点击方法 */ -( void )customButton{ [self.lblDish setText:self.txtMaterial.text];}
UIButton 的几种触发方式: 1 、 UIControlEventTouchDown
指鼠标左键按下(注:只是 “ 按下 ” )的动作 2 、 UIControlEventTouchDownRepeat
指鼠标左键连续多次重复按下(注:只是 “ 按下 ” )的动作,比如,鼠标连续双击、三击、 ...... 、多次连击。
说明:多次重复按下时,事件序列是这样的:
UIControlEventTouchDown ->
(UIControlEventTouchUpInside) ->
UIControlEventTouchDown ->
UIControlEventTouchDownRepeat ->
(UIControlEventTouchUpInside) ->
UIControlEventTouchDown ->
UIControlEventTouchDownRepeat ->
(UIControlEventTouchUpInside) ->
......
除了第一次按下外,后面每次摁下都是一个 UIControlEventTouchDown 事件,然后紧跟一个 UIControlEventTouchDownRepeat 事件。
3 、 UIControlEventTouchDragInside 指按下鼠标,然后在控件边界范围内拖动。
4
、
UIControlEventTouchDragOutside
与
UIControlEventTouchDragInside
不同的是,拖动时,鼠标位于控件边界范围之外。
但首先得有个 UIControlEventTouchDown 事件,然后接一个 UIControlEventTouchDragInside 事件,再接 一个 UIControlEventTouchDragExit 事件,这时,鼠标已经位于控件外了,继续拖动就是 UIControlEventTouchDragOutside 事件了。
具体操作是:在控件里面按下鼠标,然后拖动到控件之外。 5 、 UIControlEventTouchDragEnter
指拖动动作中,从控件边界外到内时产生的事件。 6 、 UIControlEventTouchDragExit
指拖动动作中,从控件边界内到外时产生的事件。 7 、 UIControlEventTouchUpInside
指鼠标在控件范围内抬起,前提先得按下,即 UIControlEventTouchDown 或 UIControlEventTouchDownRepeat 事件。
8
、
UIControlEventTouchUpOutside
指鼠标在控件边界范围外抬起,前提先得按下,然后拖动到控件外,即
UIControlEventTouchDown
->
UIControlEventTouchDragInside(n
个
) ->
UIControlEventTouchDragExit ->
UIControlEventTouchDragOutside(n
个
)
时间序列,再然后就是抬起鼠标,产生
UIControlEventTouchUpOutside
事件。
事例传送门: TargetActionPattern 参考:
1 、 http://developer.apple.com/library/ios/#documentation/general/conceptual/Devpedia-CocoaApp/TargetAction.html
2 、 http://blog.teamtreehouse.com/ios-design-patterns-target-action-part-1
3 、 http://developer.apple.com/library/ios/#documentation/uikit/reference/UIControl_Class/Reference/Reference.html
原文:http://blog.csdn.net/zhaoguodongios/article/details/44095417
内容总结
以上是互联网集市为您收集整理的iOS 对于Target-Action设计模式的理解全部内容,希望文章能够帮你解决iOS 对于Target-Action设计模式的理解所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。