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