[Objective-c] 015_delegate (principal agent)

Source: Internet
Author: User
Tags ming

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)

Related Article

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.