The delegate model has many applications in both the cocoa touch framework and the cocoa framework. For example, a class is required when the app starts: UIApplication. Inside the program's entry function, the main function:
int Main (intChar * argv[]) { @autoreleasepool { return Class]));} }
The UIApplication method Uiapplicationmain function sets its delegate class: Wbappdelegate. UIApplication is not directly dependent on the Wbappdelegate class, but relies on the Uiapplicationdelegate protocol, Wbappdelegate class implements the Protocol Uiapplicationdelegate, Wbappdelegate is a delegate class.
The delegate is to reduce the complexity and coupling of an object, so that it can be more versatile and put some processing in the delegate object to be encoded. The generic class becomes a framework class because of commonality, and the framework class keeps a pointer to the delegate object and sends a message to the delegate object at a particular moment. A message might simply notify the delegate object to do something, or it might be a control over the delegate object.
The following is an example of a class teacher and monitor to introduce the principle of the implementation of the Commission model and application scenarios. Teacher (Teacher) in order to better manage the class needs to appoint a classmate as monitor, the teacher hopes that the monitor can help him do two things:
1, release some information (sendMessage)
2, collect some students ' situation (collectsomething)
That is to say, the teacher needs to entrust these two things to the monitor to do a better job, but to become a monitor, we need to implement an agreement that requires the ability to deal with "releasing some information" and "collecting some of the students ' situations." Define a teacher class first:
#import @protocol wbteacherdelegate@required -(void ) SendMessage; -(void @end @interface Wbteacher:nsobject@property (nonatomic, weak) id delegate @end
// // // Designpatternsdemo // // // Copyright (c) Han Xuepeng 2015. All rights reserved. // #import " wbteacher.h " @implementation Wbteacher @end
The teacher class is the generic class mentioned above, which keeps a reference to the delegate object through the delegate attribute, the delegate object is the monitor, and he needs to implement the Protocol wbteacherdelegate. The Wbteacherdelegate agreement provides for two methods: SendMessage and collectsomething.
The following to achieve the Monitor class:
// // WBMonitor.h// designpatternsdemo//// Created by Han Xuepeng on 15/6/22. // Copyright (c) 2015 Han Xuepeng. All rights reserved. // #import #import " WBTeacher.h " @interface @end
////WBMONITOR.M//Designpatternsdemo////Created by Han Xuepeng on 15/6/22.//Copyright (c) 2015 Han Xuepeng. All rights reserved.//#import "WBMonitor.h"@implementationWbmonitor- (ID) init { self=[Super Init]; Wbteacher*teacher =[[Wbteacher alloc] init]; Teacher.Delegate=Self ; [Teacher DoSomething:0]; [Teacher DoSomething:1]; returnSelf ;}- (void) sendMessage {NSLog (@"monitor:send Message ...");}- (void) collectsomething {NSLog (@"monitor:collect something ...");}@end
Then modify the Teacher class:
////WBTeacher.h//Designpatternsdemo////Created by Han Xuepeng on 15/6/22.//Copyright (c) 2015 Han Xuepeng. All rights reserved.//#import @protocolwbteacherdelegate@required- (void) SendMessage;- (void) collectsomething;@end@interfaceWbteacher:nsobject@property (nonatomic, weak)ID Delegate;- (void) DoSomething: (int) tag;@end////WBTEACHER.M//Designpatternsdemo////Created by Han Xuepeng on 15/6/22.//Copyright (c) 2015 Han Xuepeng. All rights reserved.//#import "WBTeacher.h"@implementationWbteacher- (void) DoSomething: (int) Tag {if(!_delegate) { return; } Switch(tag) { Case 0: [_delegate sendMessage]; Break; Case 1: [_delegate collectsomething]; Break; default: Break; }}@end
As you can see, the delegate protocol Wbteacherdelegate defines two methods, and he has to implement classes that are wbmonitor. The dosometing is defined in the Wbteacher class: The method for debugging is convenient for the simulation teacher to send messages to the monitor.
The delegate object and the general class establish the relationship through teacher.delegate = self in the Init method in the Wbmonitor class. The debug output reads as follows:
--£ º47.991 designpatternsdemo[26365: 607 ] Monitor:send message ... --£ º47.993 designpatternsdemo[ 26365:607] monitor:collect something ...
With Wbteacher and Wbmonitor classes: Delegate mode
The delegate mode for iOS common design patterns