iOS 開發之Target-action模式Target-action:目標-動作模式,它貫穿于 iOS 開發始終。但是對初學者來講,還是被這類模
式弄得1頭霧水。
其實 Target-action 模式很簡單,就是當某個事件產生時,調用那個對象中的那個方法。如:按下按鈕時,調用 Controller 里邊的 click 方 法。“那個對象”就是 Target,“那個方法”就是 Action,及 Controller 是 Targer,click 方法是 action。
1般 Target 都是 Controller,而 Action 有它自己固有的格式:-(IBAction)click:(id)sender。以下圖所示,target 是處理交互事件的對象實例,action 是 target 對象中處理該事件的方法。
這里有兩種方式給“炒菜”按鈕設置 Action:1、直接拖拽連線
2、以代碼的方式實現
在 iOS 中有1個
UIControl
類,該類中定義了1個
-(void)addTarget:(id)target action:(SEL)forControlEvents:(UIControlEvents)controlEvents
方法,大部份視圖類都繼承自 UIControl 類,所以"炒菜"按鈕可使用該方法實現 Target-action模式。在 iOS 中這類設計模式被稱作1個對象給另外1個對象發送消息。
- (void)viewDidLoad{ [super viewDidLoad]; // 給炒菜按鈕添加點擊事件 // 使用 Target-action 設計模式,在兩個對象間直接發送消息[self.btnCooking addTarget:self action:@selector(pressCooking:)forControlEvents:UIControlEventTouchUpInside];}
1、self 指目標對象為當前對象,及 WViewController;
2、action 即 在目標對象上的點擊方法;
3、什么時候調用該方法,UIControlEventTouchUpInside 即單擊時。
“炒菜”按鈕是1個可交互的視圖控件,點擊它后,它指定了1個 target(目標對象),并履行目標對象上指定的 action(方法)。
action 方法有以下幾種情勢:
- (void)doSomething;// OR- (void)doSomething:(id)sender;// OR-(IBAction)doSomething:(id)sender;// OR-(IBAction)doSomething:(UIButton *) sender;
這里的 sender,發送者,就是對 “菜單” 按鈕對象的援用。以下代碼是完全用代碼定義的1個 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
指鼠標左鍵連續屢次重復按下(注:只是“按下”)的動作,比如,鼠標連續雙擊、3擊、......、屢次連擊。
說明:屢次重復按下時,事件序列是這樣的:
UIControlEventTouchDown ->
(UIControlEventTouchUpInside) ->
UIControlEventTouchDown ->
UIControlEventTouchDownRepeat ->
(UIControlEventTouchUpInside) ->
UIControlEventTouchDown ->
UIControlEventTouchDownRepeat ->
(UIControlEventTouchUpInside) ->
......
除第1次按下外,后面每次摁下都是1個UIControlEventTouchDown事件,然后緊跟1個UIControlEventTouchDownRepeat 事件。
3、UIControlEventTouchDragInside指按下鼠標,然后在控件邊界范圍內拖動。
4、UIControlEventTouchDragOutside
與 UIControlEventTouchDragInside
不同的是,拖動時,鼠標位于控件邊界范圍以外。
但首先得有個 UIControlEventTouchDown 事件,然后接1個 UIControlEventTouchDragInside事件,再接 1個 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⑴
3、http://developer.apple.com/library/ios/#documentation/uikit/reference/UIControl_Class/Reference/Reference.html