1. Scenario Description
In the iOS development process, is generally uiviewcontroller to control the behavior of UIView, generally very little direction interaction, that is, the uiview of the characteristics of the call Uiviewcontroller the corresponding method, in the face of this situation, Most of them use a uiviewcontroller pointer in a subclass of UIView, which can be achieved when UIView needs to invoke the Uiviewcontroller method.
2. Solution
This method is effective (c/s), but always feel not elegant, there is no other way to achieve it, the answer is yes, it is estimated that many people have already thought, that is commissioned (Delegate), In fact, many of the interface elements in iOS use this way to interact with the Uiviewcontroller, which gives the feeling more "apple" some!
3. Key points of knowledgeDelegation is one of the simplest and most flexible patterns in cocoa. A delegate is a behavior that gives an object an opportunity to react to a change in another object, or to affect another object. The basic idea is that two objects work together to solve the problem. An object is very common and intended to be reused in a wide range of situations. It stores a reference to another object (that is, its delegate) and sends a message to the delegate at the critical moment. A message may simply notify the delegate that something has happened, give the delegate an opportunity to perform additional processing, or the message may require the delegate to provide some critical information to control what is happening.
The delegation model can play two roles:
First: The delegate assists the object body to complete an operation, and it will need to customize the operation through the delegate object to customize the implementation, to achieve the same role as the sub-class object body.
Second: Event listener, the delegate object listens to some important events of the object body, makes the concrete response to the event or broadcasts the event to the object that needs to respond.
the advantages of a personal understanding of the use of a delegation model are:
A, to avoid the sub-class of too many subclasses and the child class and the parent class coupling
B. Hierarchical decoupling via delegated messaging mechanism
4. See Code 4.1 Implementing the pre-delegation code 4.1.1 MainViewController.h
mainviewcontroller.h// wellloggraph//// Created by Wanglei on 13-8-23.// Copyright (c) 2013 Wanglei. All rights reserved.//#import <UIKit/UIKit.h> @interface mainviewcontroller:uiviewcontroller@end
4.1.2 MAINVIEWCONTROLLER.M
mainviewcontroller.m// wellloggraph//// Created by Wanglei on 13-8-23.// Copyright (c) 2013 Wanglei. All rights reserved.//#import "MainViewController.h" #import "WellLogMapView.h" @interface Mainviewcontroller () @ End@implementation mainviewcontroller-(ID) initwithnibname: (NSString *) Nibnameornil Bundle: (NSBundle *) nibbundleornil{self = [super Initwithnibname:nibnameornil Bundle:nibbundleornil]; if (self) { //Custom initialization } return to self;} -(void) viewdidload{ [Super viewdidload];//do no additional setup after loading the view. } -(void) Viewwillappear: (BOOL) animated{ welllogmapview* v = (welllogmapview*) self.view; [v Setneedsdisplay];} -(void) didreceivememorywarning{ [Super didreceivememorywarning]; Dispose of any resources the can be recreated.} @end
4.1.3Welllogmapview.H
welllogmapview.h// wellloggraph//// Created by Wanglei on 13-8-24.// Copyright (c) 2013 Wanglei . All rights reserved.//#import <UIKit/UIKit.h> @interface welllogmapview:uiview{ } @end
4.1.4 WELLLOGMAPVIEW.M
welllogmapview.m// wellloggraph//// Created by Wanglei on 13-8-24.// Copyright (c) 2013 Wanglei . All rights reserved.//#import "WellLogMapView.h" @implementation welllogmapview-(ID) initWithFrame: (CGRect) frame{ Self = [Super Initwithframe:frame]; if (self) { //initialization code } return self;} Only override Drawrect:if-perform custom drawing.//an empty implementation adversely affects performance during a nimation.-(void) DrawRect: (cgrect) rect{ //Drawing code }-(void) Touchesbegan: (Nsset *) touches withevent: ( Uievent *) event{ }-(void) touchesended: (Nsset *) touches withevent: (uievent *) event{ //uitouch *touch = [ Touches Anyobject]; Want to tell Mainviewcontroller here to do some activities, that is, how to call the Mainviewcontroller method } @end
4.2 Code 4.2.1 After implementing the delegateMainViewController.h
mainviewcontroller.h// wellloggraph//// Created by Wanglei on 13-8-23.// Copyright (c) 2013 Wanglei. All rights reserved.//#import <UIKit/UIKit.h> @protocol welllogmapviewdelegate-(void) onselectionchanged: (ID) selection; @end @interface mainviewcontroller:uiviewcontroller<welllogmapviewdelegate>-(void) OnSelectionChanged: (ID) selection; @end
4.2.2 MAINVIEWCONTROLLER.M
mainviewcontroller.m//wellloggraph////Created by Wanglei on 13-8-23.//Copyright (c) 2013 Wanglei. All rights reserved.//#import "MainViewController.h" #import "WellLogMapView.h" @interface Mainviewcontroller () @ End@implementation mainviewcontroller-(ID) initwithnibname: (NSString *) Nibnameornil Bundle: (NSBundle *) nibbundleornil{self = [super Initwithnibname:nibnameornil Bundle:nibbundleornil]; if (self) {//Custom initialization} return to self;} -(void) viewdidload{[Super viewdidload];//do any additional setup after loading the view. Self.navigationItem.rightBarButtonItem.title = @ "Add columns"; [Self.navigationItem.rightBarButtonItem Initwithbarbuttonsystemitem:uibarbuttonsystemitemundo Target:self Action: @se Lector (myaction)]; welllogmapview* view = (welllogmapview*) Self.view; [View setdelegate:self];} -(void) viewWillappear: (BOOL) animated{welllogmapview* v = (welllogmapview*) Self.view; [v Setneedsdisplay];} -(void) didreceivememorywarning{[Super didreceivememorywarning]; Dispose of any resources the can be recreated.} -(void) OnSelectionChanged: (ID) selection{//do something} @end
4.2.3 WellLogMapView.h
welllogmapview.h// wellloggraph//// Created by Wanglei on 13-8-24.// Copyright (c) 2013 Wanglei . All rights reserved.//#import <UIKit/UIKit.h> #import "MainViewController.h" @interface welllogmapview:uiview{ id<welllogmapviewdelegate> Delegate;} @property (nonatomic, retain) ID delegate; @end
4.2.4 WELLLOGMAPVIEW.M
welllogmapview.m// wellloggraph//// Created by Wanglei on 13-8-24.// Copyright (c) 2013 Wanglei . All rights reserved.//#import "WellLogMapView.h" @implementation welllogmapview@synthesize delegate;-(ID) initWithFrame: (cgrect) frame{self = [Super Initwithframe:frame]; if (self) { //initialization code } return self;} Only override Drawrect:if-perform custom drawing.//an empty implementation adversely affects performance during a nimation.-(void) DrawRect: (cgrect) rect{ //Drawing code }-(void) touchesended: (Nsset *) touches withevent: ( Uievent *) event{ //uitouch *touch = [touches anyobject]; [Delegate Onselectionchanged:nil]; } @end
Two-way interaction between Uiviewcontroller and UIView in iOS tips