In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you what is the use of the Target-Action model developed by IOS. I hope you will get something after reading this article. Let's discuss it together.
The main purpose of this mode is to reduce the code coupling between modules and enhance the cohesion between codes within modules.
Let's look at an example:
1: suppose there is a requirement that when we click on a view object, we can change the color of the view, which is very easy for beginners to do, as long as we rewrite it in this view class:
-(void) touchesBegan: (NSSet *) touches withEvent: (UIEvent *) event function, and then change the background color of the view, but at this time there is a new requirement. Some people need to change the color of the view when clicking on the view, and some people need to change the position of the view when clicking on the view. In order to let different objects execute different events, when instantiating the view class object, you need to specify the events that the object is interested in. For this requirement, we can define enumerated variables as data members of the object, specify enumerated values during initialization (that is, specify events of interest), and override the-(void) touchesBegan: (NSSet *) touches withEvent: (UIEvent *) event function to perform different functions for different enumerated values. Suppose we need to perform a flip function when clicking on the view object. We have to modify the specific implementation functions in the view, so that the coupling between the codes is relatively large, and it is not convenient to transplant (imagine a scenario in which other people's app requires the view class you have written, but others do not need the event methods in your view class, so they need to modify the view class, and some errors will inevitably occur). The solution to this problem is Target-Action mode and look directly at the code:
/ / main view header file
# import @ interface MainViewController: UIViewController@end
/ / implementation of the main view
# 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 green:green/255.0 blue:blue/255.0 alpha:1.0];}-(void) moveFrame: (UIView *) aView {aView.frame = CGRectMake (arc4random ()% 320, arc4random ()% 480,100,100);} @ end
/ / Test view class header file
# import @ 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 after reading this article, I believe you have a certain understanding of "what is the use of the Target-Action model developed by IOS". If you want to know more about it, welcome to follow the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.