Target -- action design mode and delegate Design Mode

Source: Internet
Author: User

Target -- action design mode and delegate Design Mode

1. tar get -- action design mode 1. create a View Controller in the View Controller. in the m file, create the CustomView (subclass of UIView) object CustomView * blueView = [[CustomView alloc] initWithFrame: CGRectMake (20,180,280,100)]; blueView. backgroundColor = [UIColor blueColor]; [blueView addTarget: self action: @ selector (changeLocat :) forControlEvents: UIControlEventTouchUpInside]; [self. view addSubview: blueView]; [blueView release]; 2. in CustomView. in the hfile, define the method-(void) addTarget :( id) target action :( SEL) action forControlEvents :( UIControlEvents) controlEvents; (this method is similar to that in the UIButton class) 3. in CustomView. in the m file, @ interface CustomView () {id _ target; SEL _ action; UIControlEvents _ controlEvents;} @ end @ implementation CustomView-(id) initWithFrame :( CGRect) frame {self = [super initWithFrame: frame]; if (self) {// Initialization code} return self;}-(void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) event {when the current view receives a touch event, it is handed over to the target to process if (UIControlEventTouchDown ==_ controlEvents) {[_ target extends mselector: _ action withObject: self] ;}} -(void) touchesEnded :( NSSet *) touches withEvent :( UIEvent *) event {if (UIControlEventTouchUpInside = _ controlEvents) {[_ target extends mselector: _ action withObject: self];} -(void) touchesMoved :( NSSet *) touches withEvent :( UIEvent *) event {} specifies that after the current view receives a response event, target is available to respond using the action method-(void) addTarget :( id) target action :( SEL) action forControlEvents :( UIControlEvents) controlEvents {// use instance variables to store external input parameters, _ target = target; _ action = action; _ controlEvents = controlEvents;} 4. in. in the m file, the call method-(void) changeSupColor :( CustomView *) view {view. superview. backgroundColor = [UIColor random];} 2. delegate design mode 1. the idea of the proxy design mode: for the current view object, it is only responsible for receiving touch events. When a touch event occurs, the notification proxy will handle the event accordingly. How can the proxy handle the event? The view does not care. 2. create view control in its. in the m file, define TouchView * redView = [[TouchView alloc] initWithFrame: CGRectMake (20,100,280,100)]; redView. backgroundColor = [UIColor redColor]; [self. view addSubview: redView]; redView. tag = 100; // specify the proxy redView for the view. delegate = self; [redView release]; 3. in the TouchView. h file, defining protocols, methods, and attributes @ class TouchView; @ protocol TouchViewDelegate
 
  
// There are four methods in the protocol, which correspond to four touch times. When the view object receives different touch events, it notifies the proxy object through the methods in the Protocol. @ optional-(void) preview :( TouchView *) touchView;-(void) touchViewTouchesMoved :( TouchView *) touchView;-(void) preview :( TouchView *) touchView;-(void) touchViewTouchesCancelled :( TouchView *) touchView; @ end @ interface TouchView: UIView @ property (nonatomic, assign) id
  
   
Delegate; @ end4. in TouchView. m file proxy design mode: for the current view object, it is only responsible for receiving touch events. When a touch event occurs, it notifies the proxy to handle the event. How does the proxy handle the event? The view does not care. (1 ). -(void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) event {// principle: // If the proxy implements the method in the corresponding protocol, let the proxy call the method, if no method is implemented, do not call // determine whether the proxy has implemented the corresponding method (determine whether an object has implemented a specific party) if ([self. delegate respondsToSelector: @ selector (touchViewTouchesBegan :)]) {// notify the proxy to perform corresponding operations when the touch starts. delegate touchViewTouchesBegan: self] ;}} (2 ). -(void) touchesEnded :( NSSet *) touches withEvent :( UIEvent *) event {if ([self. delegate respondsToSelector: @ selector (touchViewTouchesEnded :)]) {// when the touch ends, notify the proxy to perform the corresponding operation [self. delegate touchViewTouchesEnded: self] ;}} (3 ). -(void) touchesMoved :( NSSet *) touches withEvent :( UIEvent *) event {if ([self. delegate respondsToSelector: @ selector (touchViewTouchesMoved :)]) {// notify the proxy to perform the corresponding operation when the mobile phone is touched [self. delegate touchViewTouchesMoved: self] ;}} (4 ). -(void) touchesCancelled :( NSSet *) touches withEvent :( UIEvent *) event {if ([self. delegate respondsToSelector: @ selector (touchViewTouchesCancelled :)]) {// when the touch is canceled, the proxy is notified to perform the corresponding operation [self. delegate touchViewTouchesCancelled: self] ;}} 5. in. m file, implementation method.
  
 

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.