Target-action Mode of IOS development

Source: Internet
Author: User
Tags button type uicontrol

Target-action: Target-action mode, which runs through iOS development always. But for beginners, it is still confused by this pattern.

In fact, the target-action pattern is simple, that is, when an event occurs, call that method in that object. For example: When the button is pressed, the click Method inside the controller is called. "That object" is target, "that method" is the action, and the controller is the Targer,click method is action.

The general target is the controller, and the action has its own inherent format:-(ibaction) Click: (ID) sender.

As shown, target is an object instance that handles interactive events, and the action is the method that handles the event in the target object.

Here are two ways to set the action for the Stir button:

1. Direct Drag and drop connection

2, in the way of code to achieve

There is a Uicontrol class in iOS that defines a

-(void) AddTarget: (ID) Target action: (SEL) forControlEvents: (uicontrolevents) controlevents

method, most of the view classes inherit from the Uicontrol class, so the "stir fry" button can use this method to implement Target-action mode. In iOS, this design pattern is called an object that sends a message to another object.

-(void) viewdidload{    [Super Viewdidload];        Add a click event to the Stir-fry button/    /use target-action design mode to send a message directly between two objects    [self.btncooking addtarget:self action: @selector ( Presscooking:) forcontrolevents:uicontroleventtouchupinside];}

1, self refers to the target object is the current object, and Wviewcontroller;

2. Action is the click method on the target object;

3, when the method is called, UIControlEventTouchUpInside is clicked.

The Stir-fry button is an interactive view control that, when clicked, specifies a target object and executes the specified action (method) on the target object.

The action method has several forms:

-(void) dosomething;//or-(void) dosomething: (ID) sender;//or-(ibaction) dosomething: (ID) sender;//or-(ibaction) DoSomething: (UIButton *) sender;

The sender here, the sender, is the reference to the menu button object.

The following code is a UIButton that is completely defined in code and added in Self.view:

-(void) viewdidload{    [Super Viewdidload];        Instantiate the button and set the button type to fillet    UIButton *btncustom = [UIButton buttonwithtype:uibuttontyperoundedrect];    Set Button size    btncustom.frame = CGRectMake (124, N, a, a);    Set button title    [Btncustom settitle:@ "Click on Me ..." forstate:uicontrolstatenormal];    Set button click event    [Btncustom addtarget:self Action: @selector (CustomButton) forControlEvents: UIControlEventTouchUpInside];    Add a button to view    [Self.view addsubview:btncustom];} /** Custom button click Method */-(void) custombutton{    [Self.lbldish setText:self.txtMaterial.text];}

Several triggering methods of UIButton:

1, Uicontroleventtouchdown

The left mouse button press (note: just "press") the action

2, Uicontroleventtouchdownrepeat

The left mouse button repeatedly pressed (note: just "press") action, for example, the mouse double-click, three-hit 、......、 multiple combos.

Note: When repeatedly pressed, the sequence of events is this:

Uicontroleventtouchdown

(UIControlEventTouchUpInside)

  Uicontroleventtouchdown

  Uicontroleventtouchdownrepeat

  (UIControlEventTouchUpInside)

  Uicontroleventtouchdown

< EM id= "__mcedel" > UICONTROLEVENTTOUCHDOWNREPEAT&NBSP;->&NBSP;

  (UIControlEventTouchUpInside)

......

< EM id= "__mcedel" > except for the first press, Each subsequent press is a Uicontroleventtouchdown event followed by a Uicontroleventtouchdownrepeat event.

< EM id= "__mcedel" > 3, uicontroleventtouchdraginside

< EM id= "__mcedel" > refers to pressing the mouse and then dragging within the bounds of the control.

< EM id= "__mcedel" > 4, Uicontroleventtouchdragoutside

  Unlike Uicontroleventtouchdraginside, when you drag, the mouse is outside the bounds of the control.

But first there must be a Uicontroleventtouchdown event, then a Uicontroleventtouchdraginside event followed by a Uicontroleventtouchdragexit event, The mouse is already outside the control, and continuing to drag is the Uicontroleventtouchdragoutside event.

< EM id= "__mcedel" > The specific action is: Press the mouse inside the control, and then drag outside the control.

< EM id= "__mcedel" > 5, Uicontroleventtouchdragenter

< EM id= "__mcedel" >

< EM id= "__mcedel" > 6, uicontroleventtouchdragexit

< EM id= "__mcedel" >

7, UIControlEventTouchUpInside

8, Uicontroleventtouchupoutside

Uicontroleventtouchdown

Uicontroleventtouchdraginside (N)

Uicontroleventtouchdragexit

Uicontroleventtouchdragoutside (N)

Time series, and then just lift up the mouse to generate the Uicontroleventtouchupoutside event.

Case Portal: Targetactionpattern

Reference:

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

Copyright notice: This article by http://wzrong.cnblogs.com or http://iostour.diandian.com original, welcome reprint share. Please respect the author Labor, reproduced when the statement and the author of the blog link, thank you!

Original article, if you want to reprint please indicate the source, thank you!

Original address: http://www.cnblogs.com/wzrong/p/3218867.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.