This pattern is primarily intended to reduce code coupling between modules and to enhance cohesion within the code within the module.
Let's take a look at an example:
1: Suppose there is a requirement: we can change the color of the view by clicking on a View object, which is a very easy task for beginners, as long as it is overridden in this view class:
-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) event function, Then change the background color of the view, but then there is a new demand, some people need to click on the view to change the color of the view, some people need to click on the view to change the location of the view, in order to allow different objects to perform different events, when instantiating the view class object, you need to specify the object of interest to the event, For this requirement we can define the enumeration variable as the data member of the object and specify the enumeration value at initialization (that is, specify the event of interest), and you need to override -(void) Touchesbegan: (nsset *) touches withevent: (uievent *) event function, let it perform different functions for different enumeration values, assuming that at this time we need to do a flip function when we click on the View object, we have to modify the specific implementation function within the view, so that the coupling between the code is relatively large, it is inconvenient to transplant (imagine a scenario like this, Suppose someone else's app needs you to write this view class, but others do not need your view class in the event method, you need to modify the view class, inevitably some errors occur), the solution to this problem is target-action mode, directly look at the code:
Main View Header File
#import <UIKit/UIKit.h> @interface Mainviewcontroller:uiviewcontroller@end
Main View Implementation
#import "MainViewController.h" #import "MyView.h" @implementation mainviewcontroller-(ID) init{ self= [super init]; if (self) { } return self;} -(void) Viewdidload{ myview * view1 = [[myview alloc] Initwithframe:cgrectmake (10, 20, 100, 100) andtarget:self andaction: @selector ( ChangeColor:)]; [self.view addsubview:view1]; myview * view2 = [[myview alloc]initwithframe: CGRectMake (10, 20, 100, 100) andtarget:self andaction: @selector (moveFrame:)]; [self.view addsubview:view2]; }-(void) ChangeColor: (UIView * ) aview{ nslog (@ "ButtonClick"); int red = arc4random ()%255; int green = arc4random ()%255; int blue = Arc4random ()%255; aview.backgroundcolor = [uicolor colorwithred:red/ 255.0&NBSP;GREEN:GREEN/255.0&NBSP;BLUE:BLUE/255.0&NBSP;ALPHA:1.0];} -(void) Moveframe: (uiview *) Aview{ aview.frame = cgrectmake (arc4random ()% 320, arc4random ()%480, 100, 100);} @end
Test View class header file
#import <UIKit/UIKit.h> @interface myview:uiview{ID _target; SEL _action;} -(ID) initWithFrame: (CGRect) frame andtarget: (ID) Target andaction: (SEL) Action; @property (assign,readwrite,nonatomic ) ID deledate; @end
//Test view class implementation
#import "MyView.h" @implementation myview-(ID) initWithFrame: ( CGRect) Frame andtarget: (ID) target andaction: (SEL) action{ self = [ super initwithframe:frame]; if (self) { _target = target; _action = action; } self.backgroundcolor = [uicolor bluecolor]; return self;} -(void) Touchesbegan: (nsset *) Touches withevent: (uievent *) event{ [_ Target performselector:_action withobject:self];} @end
Summary: From the above code can be seen in the module and the coupling between the module is very low, no matter what you want to add the function of the event, do not need to modify the view class!
This article is from the Web Learning Summary blog, so be sure to keep this source http://8947509.blog.51cto.com/8937509/1575847
Target-action Mode of iOS development