iOS 开发之Target-Action
来源:互联网 发布:免费数据恢复软件 编辑:程序博客网 时间:2024/05/16 06:49
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 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; // 设置按钮大小 btnCustom.frame = CGRectMake(124, 140, 73, 44); // 设置按钮标题 [btnCustom setTitle:@"点击我..." forState:UIControlStateNormal]; // 设置按钮点击事件 [btnCustom addTarget:self action:@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
- iOS 开发之Target-Action
- iOS 开发之Target-action模式
- iOS 开发之Target-action模式
- iOS 开发之Target-action模式
- iOS 开发之Target-action模式
- iOS 开发之Target-action模式
- iOS 开发之Target-action模式
- iOS 开发之Target-action模式/模拟器鼠标事件常量
- iOS 开发之Target
- iOS 开发之Target
- IOS开发 Target-Action方法的实现
- iOS开发 -- 耦合 ,target/action设计模式
- 【ios开发】浅谈Target-action模式
- iOS设计模式之Target-Action
- iOS开发之Target-action模式(点击按钮,滑动滑块等)
- iOS Target-Action
- iOS Target-Action
- iOS target-action模式
- Linux系统信息查看命令大全
- 【java】synchronized修饰静态方法和非静态方法的区别
- Hive配置项的含义详解
- C语言 - 自创题目:字符串输入的计算器(加减乘除计算器)
- cocos2d-x 截图保存到android端的坑啊 ---cocos2d-x学习之路[2]
- iOS 开发之Target-Action
- Qt phonon多媒体框架
- windowss编程之旋转风车
- 用hMailServer为PHP的mail()函数配置搭建windows平台的SMTP邮件服务器
- 反应堆模式ACE_Reactor类
- linux中wc命令
- 易语言-API 取窗口或者组件句柄的 屏幕坐标并限制区域 GetWindowRect ClipCursor getwindowrect GetClientRect WindowFromPoint
- Android Sliding Menu
- 【设计模式基础】结构型模式 - 2 - 外观(Facade)