Delegate is ubiquitous in iOS development, delegate is a powerful software architecture design concept, its function is that one object in the program represents another object, or one object works with another object (such as Xiao Ming likes a girl like a flower, but suffers from no contact with the flower, So applauded brother small east to take flowers contact way, small East classmates a day later return results to Xiaoming, .... )。 Xiao Ming can successfully chase the flowers, small east in which did something, the following step by step decomposition.
1. Create a delegate (via protocol)
#import <Foundation/Foundation.h>
@protocol phonenumberdelegate <NSObject> @required-(NSString *) Goddessphonenumber; @end
2. Declare a delegate (xiaoming)
@interface Student:person <StudentProtocol> @property (nonatomic, assign) id<phonenumberdelegate> delegate;-(ID) initwithname: (NSString *) name sex: (NSString *) Sex Age: (int.) age; @end
3. Implement the Principal-agent method (small east)
#import "Person.h" @implementation person@synthesize name = _name,sex = _sex; @synthesize age = _age;-(NSString *) GODDESSPH Onenumber { NSLog (@ "is rushing to like a flower home, to the flower mother to inquire about the number of flowers ... 5 minutes to get it. If the flower number 13888888888 "); return @ "13888888888";} @end
4. Set up the delegate agent (Xiaoming---small east), and invoke the proxy method
#import "AppDelegate.h" #import "Person.h" #import "Student.h" @interface appdelegate () @end @implementation appdelegate-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (NSDictionary *) launchoptions { Student *xiaoming = [[Student alloc] init]; Person *xiaodong = [[Person alloc] init]; Set the principal agent xiaoming.delegate = Xiaodong; Execute the principal-agent method NSString *phonenumber = [Xiaodong goddessphonenumber]; Keep the goddess number xiaoming.goddessphonenumber = PhoneNumber; return YES;} @end
Delegate is so easy to use, Xiao Ming's wish to get the number like flowers.
2015-08-10 21:20:56.516 attendance[56285:1842177] is rushing to like a flower home, to the flower mother to inquire like the number of flowers ... 5 minutes to get it . as the number of flowers 13888888888
2015-08-10 21:20:56.517 attendance[56285:1842177] Xiaoming finally got the flower number:13888888888
This site article is for baby bus SD. Team Original, reproduced must be clearly noted: (the author's official website: Baby bus )
Reprinted from "Baby bus Superdo Team" original link: http://www.cnblogs.com/superdo/p/4719244.html
[Objective-c] 015_delegate (principal agent)