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對象中處理該事件的方法。

27010535-ac3623b5132f4026ac8552247a4b7cf2.png.jpeg

這里有兩種方式給“炒菜”按鈕設置Action:

1、直接拖拽連線


27010719-1096542bfcae4545bf2afc48f95f6861.png.jpeg

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];
}

27003050-556548af74e74b84a647e302ea32ad12.png.jpeg

1、self 指目標對象為當前對象,及ViewController;

2、action 即 在目標對象上的點擊方法;

3、何時調用該方法,UIControlEventTouchUpInside即單擊時。

“炒菜”按鈕是一個可交互的視圖控件,點擊它后,它指定了一個target(目標對象),并執行目標對象上指定的action(方法)。

action方法有以下幾種形式:

// 無參數無返回值
- (void)doSomething;
// 有參數無返回值
- (void)doSomething:(id)sender;
// 無參數有返回值
- (IBAction)doSomething;
// 有參數有返回值
- (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事件。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容