Target/action design mode simple to use

Source: Internet
Author: User

target-action mode is OBJC method calls between very common objects in the , but OBJC The method call is called Send Message.

a bunch of situations in and UI Dealing with a variety of GUI This mode is used by the events on the . corresponding to the . NET on the processing mode is delegate/event the . but , Target-action Bye C given by the language , It's a lot more flexible . , no checks at compile time , are bindings at run time .

Code Demo:

1. Create a class that inherits UIView

#import <UIKit/UIKit.h> @interface touchview:uiview//the @property (nonatomic,assign) ID of the button of the analog//implementation method Target;//The Method of implementation @property (nonatomic,assign) SEL action; @end

Implementation section:

@implementation touchview-(void) Touchesbegan: (Nsset *) touches withevent: (uievent *) event{    //self.target Method of implementing Self.action    //withobject: Generally used to achieve communication between multiple threads    [Self.target performSelector:self.action withobject:self];        } @end

Initialization

#import "RootViewController.h" #import "TouchView.h" @interface Rootviewcontroller () @end @implementation    rootviewcontroller-(void) viewdidload {[Super viewdidload];    Do any additional setup after loading the view.        Self.view.backgroundColor = [Uicolor Whitecolor]; Target/action/* Core idea of object-oriented programming: Cohesion Poly, low-coupling target/action: Can be used to decouple */TouchView *touchview1 = [[Touchvie    W alloc]initwithframe:cgrectmake (100, 100, 175, 100)];        Touchview1.backgroundcolor = [Uicolor Magentacolor];    Touchview*touchview2 = [[TouchView alloc]initwithframe:cgrectmake (100, 250, 175, 100)];        Touchview2.backgroundcolor = [Uicolor Redcolor];        Assigns a value of target touchview1.target = self;    Specify the method to implement, assign the action value touchview1.action = @selector (touchAction1:);    Touchview1.tag = 111;    [Self.view Addsubview:touchview1];            [TouchView1 release];    Touchview2.target = self;    Touchview2.action [email protected] (touchAction2:); Touchview2.tag = 222;    [Self.view Addsubview:touchview2];            [TouchView2 release];    }-(void) TouchAction1: (TouchView *) touchview{TouchView *temptouchview = (TouchView *) [Self.view viewwithtag:111]; Temptouchview.backgroundcolor = [Uicolor colorwithred:arc4random ()%256/255.0 green:arc4random ()%256/255.0 Blue: Arc4random ()%256/255.0 alpha:arc4random ()%256/255.0];}    -(void) TouchAction2: (TouchView *) touchview{TouchView *temptouchview1 = (TouchView *) [Self.view viewwithtag:222];//    Temptouchview1.center = Cgpointmake (Arc4random ()%180 +90, Arc4random ()%567 + 50);    Temptouchview1.frame = CGRectMake (Arc4random ()%201, Arc4random ()% 568, 175, 100); } @end


Target/action design mode simple to use

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.