iOS 開發(fā)之Target-action模式Target-action:目標(biāo)-動(dòng)作模式,它貫穿于 iOS 開發(fā)始終。但是對(duì)初學(xué)者來(lái)講,還是被這類模
式弄得1頭霧水。
其實(shí) Target-action 模式很簡(jiǎn)單,就是當(dāng)某個(gè)事件產(chǎn)生時(shí),調(diào)用那個(gè)對(duì)象中的那個(gè)方法。如:按下按鈕時(shí),調(diào)用 Controller 里邊的 click 方 法。“那個(gè)對(duì)象”就是 Target,“那個(gè)方法”就是 Action,及 Controller 是 Targer,click 方法是 action。
1般 Target 都是 Controller,而 Action 有它自己固有的格式:-(IBAction)click:(id)sender。以下圖所示,target 是處理交互事件的對(duì)象實(shí)例,action 是 target 對(duì)象中處理該事件的方法。
這里有兩種方式給“炒菜”按鈕設(shè)置 Action:1、直接拖拽連線
2、以代碼的方式實(shí)現(xiàn)
在 iOS 中有1個(gè)
UIControl
類,該類中定義了1個(gè)
-(void)addTarget:(id)target action:(SEL)forControlEvents:(UIControlEvents)controlEvents
方法,大部份視圖類都繼承自 UIControl 類,所以"炒菜"按鈕可使用該方法實(shí)現(xiàn) Target-action模式。在 iOS 中這類設(shè)計(jì)模式被稱作1個(gè)對(duì)象給另外1個(gè)對(duì)象發(fā)送消息。
- (void)viewDidLoad{ [super viewDidLoad]; // 給炒菜按鈕添加點(diǎn)擊事件 // 使用 Target-action 設(shè)計(jì)模式,在兩個(gè)對(duì)象間直接發(fā)送消息[self.btnCooking addTarget:self action:@selector(pressCooking:)forControlEvents:UIControlEventTouchUpInside];}
1、self 指目標(biāo)對(duì)象為當(dāng)前對(duì)象,及 WViewController;
2、action 即 在目標(biāo)對(duì)象上的點(diǎn)擊方法;
3、什么時(shí)候調(diào)用該方法,UIControlEventTouchUpInside 即單擊時(shí)。
“炒菜”按鈕是1個(gè)可交互的視圖控件,點(diǎn)擊它后,它指定了1個(gè) target(目標(biāo)對(duì)象),并履行目標(biāo)對(duì)象上指定的 action(方法)。
action 方法有以下幾種情勢(shì):
- (void)doSomething;// OR- (void)doSomething:(id)sender;// OR-(IBAction)doSomething:(id)sender;// OR-(IBAction)doSomething:(UIButton *) sender;
這里的 sender,發(fā)送者,就是對(duì) “菜單” 按鈕對(duì)象的援用。以下代碼是完全用代碼定義的1個(gè) UIButton,并添加在 self.view 中:
- (void)viewDidLoad{ [super viewDidLoad]; // 實(shí)例化按鈕,并設(shè)置按鈕類型為圓角 UIButton *btnCustom = [UIButtonbuttonWithType:UIButtonTypeRoundedRect]; // 設(shè)置按鈕大小btnCustom.frame = CGRectMake(124, 140, 73, 44); // 設(shè)置按鈕標(biāo)題[btnCustom setTitle:@"點(diǎn)擊我..." forState:UIControlStateNormal]; //設(shè)置按鈕點(diǎn)擊事件 [btnCustom addTarget:selfaction:@selector(customButton)forControlEvents:UIControlEventTouchUpInside]; // 將按鈕添加到 View[self.view addSubview:btnCustom];}/** 自定義按鈕點(diǎn)擊方法 */-(void)customButton{ [self.lblDish setText:self.txtMaterial.text];}
UIButton 的幾種觸發(fā)方式:1、UIControlEventTouchDown
指鼠標(biāo)左鍵按下(注:只是“按下”)的動(dòng)作2、UIControlEventTouchDownRepeat
指鼠標(biāo)左鍵連續(xù)屢次重復(fù)按下(注:只是“按下”)的動(dòng)作,比如,鼠標(biāo)連續(xù)雙擊、3擊、......、屢次連擊。
說(shuō)明:屢次重復(fù)按下時(shí),事件序列是這樣的:
UIControlEventTouchDown ->
(UIControlEventTouchUpInside) ->
UIControlEventTouchDown ->
UIControlEventTouchDownRepeat ->
(UIControlEventTouchUpInside) ->
UIControlEventTouchDown ->
UIControlEventTouchDownRepeat ->
(UIControlEventTouchUpInside) ->
......
除第1次按下外,后面每次摁下都是1個(gè)UIControlEventTouchDown事件,然后緊跟1個(gè)UIControlEventTouchDownRepeat 事件。
3、UIControlEventTouchDragInside指按下鼠標(biāo),然后在控件邊界范圍內(nèi)拖動(dòng)。
4、UIControlEventTouchDragOutside
與 UIControlEventTouchDragInside
不同的是,拖動(dòng)時(shí),鼠標(biāo)位于控件邊界范圍以外。
但首先得有個(gè) UIControlEventTouchDown 事件,然后接1個(gè) UIControlEventTouchDragInside事件,再接 1個(gè) UIControlEventTouchDragExit 事件,這時(shí)候,鼠標(biāo)已位于控件外了,繼續(xù)拖動(dòng)就是 UIControlEventTouchDragOutside 事件了。
具體操作是:在控件里面按下鼠標(biāo),然后拖動(dòng)到控件以外。5、UIControlEventTouchDragEnter
指拖動(dòng)動(dòng)作中,從控件邊界外到內(nèi)時(shí)產(chǎn)生的事件。6、UIControlEventTouchDragExit
指拖動(dòng)動(dòng)作中,從控件邊界內(nèi)到外時(shí)產(chǎn)生的事件。7、UIControlEventTouchUpInside
指鼠標(biāo)在控件范圍內(nèi)抬起,條件先得按下,即 UIControlEventTouchDown 或UIControlEventTouchDownRepeat 事件。
8、UIControlEventTouchUpOutside指鼠標(biāo)在控件邊界范圍外抬起,條件先得按下,然后拖動(dòng)到控件外,即UIControlEventTouchDown
->
UIControlEventTouchDragInside(n 個(gè)) ->
UIControlEventTouchDragExit ->
UIControlEventTouchDragOutside(n 個(gè))
時(shí)間序列,再然后就是抬起鼠標(biāo),產(chǎ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⑴
3、http://developer.apple.com/library/ios/#documentation/uikit/reference/UIControl_Class/Reference/Reference.html