Ios-target-action Design Pattern Understanding

Source: Internet
Author: User


Ios-target-action Design Pattern Understanding


The essence of Target-action's design pattern is that the event generated in host invokes the action method of target.

Below we use a uiview to implement the UIButton click event.


@interface custombutton:uiview-(void) Addcustombutton: (ID) Target andselector: (SEL) action; @end @implementation custombutton{    ID _target;    SEL _action;} -(void) Addcustombutton: (ID) Target andselector: (SEL) action{    if (target==_target) {        return;    }        _target = target;    _action = action;} -(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) event{}-(void) touchesended: (Nsset *) touches withevent: ( Uievent *) event{    if (_target) {        if ([_target respondstoselector:_action]) {            [_target performselector:_ Action withobject:self];}}    


Use

-(void) viewdidload {    [super viewdidload];    Additional setup after loading the view, typically from a nib.        CustomButton *btn = [[CustomButton alloc]initwithframe:cgrectmake (0, +,--)];    Btn.backgroundcolor = [Uicolor redcolor];    [Btn addcustombutton:self andselector: @selector (hello:)];    [Self.view addsubview:btn];    } -(void) Hello: (id) sender{    NSLog (@ "Hello customer");}

Run results

2015-03-29 17:09:03.803 target_action[1863:89334] Hello Customer


Running Memory state:








Ios-target-action Design Pattern Understanding

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.