Objective-c Delegate

Source: Internet
Author: User

Delegate Delegate in iOS design mode

The official documentation is explained below:

Delegation is a simple and powerful the pattern in which one object in a program acts on behalf of, or in coordination with, a Nother object. The delegating object keeps a reference to the other object-the Delegate-and at the appropriate time sends a message to it . The message informs the delegate of an event, the delegating object is about to handle or have just handled. The delegate respond to the message by updating the appearance or state of itself or other objects in the application, And in some cases it can return a value this affects how an impending event is handled. The main value of delegation is the it allows-easily customize the behavior of several objects in one central Obje Ct.

It is a simple and powerful design pattern for an object to "represent" another object and interact with other objects in the program. The main object (in this case, delegating object) maintains a reference to a proxy (delegate) and sends a message to the agent at the appropriate time. This message informs the "agent" that the main object is about to be processed or that an event has been processed. The agent can respond to the message of the event sent by the main object by updating the UI interface or other state of the object. Or, in some cases, return a value to affect how other upcoming events should be handled. The main value of the agent is that it allows you to easily customize the behavior of various objects. Note that the agent here is a noun, which is itself an object that is specifically representative of the object being proxied to deal with other objects in the program.

1 //Mandeleagte Interface Declaration2 @protocolMandeleagte <NSObject>3-(void) Onagechanged: (int) age;4 @end5 6 //Manlistener Implementation7 @interfaceManlistener:nsobject<mandeleagte>8-(void) Onagechanged: (int) age;9 @endTen  One //Manlistener Implementation A @implementationManlistener -  --(void) Onagechanged: (int) age{ theNSLog (@"Age Changed,current-is %d", age); - } - @end
1 //the protocol is equivalent to the interface in the C + + language2 @protocolIpeople <NSObject>3-(int) Getage;4-(nsstring*) GetName;5-(void) Setage: (int) age;6 @end7 8 //class defines and implements the Ipeople interface for multiple interfaces, separated by9 @interfaceMan:nsobject<ipeople>{Ten     int_age; One } A-(ID) init; --(int) Getage; --(nsstring*) GetName; the-(void) Setage: (int) age; -  -@propertyID<ManDeleagte>Delegate; -  + @end -  + //class Implementation A @implementationMans at-(int) getage{ -     return_age; - } -  --(instancetype) init{ -Self =[Super init]; in     if(self) { -Self.Delegate=Nil; to_age = -; +     } -     returnSelf ; the } *  $-(nsstring*) getname{Panax Notoginseng     return @"Jinpangpang"; - } the  +-(void) Setage: (int) age{ A     //notification to the outside world the     if(age!=_age) { +         if(self.)Delegate) { -[Self.DelegateOnagechanged:age]; $         } $     } -_age =Age ; - } the @end
1 //called2 intMainintargcChar*argv[]) {3Man *m =[[Man alloc] init];4     //assigning an delegate to an object5 [M Setdelegate:[[manlistener Alloc]init]];6[M Setage: +];7     8NSLog (@"%@", [M getName]);9NSLog (@"%d", [M getage]);Ten @autoreleasepool { One         returnUiapplicationmain (argc, argv, Nil, nsstringfromclass ([appdelegateclass])); A     } -}

//Run Results -- One- -  -: -:23.825l15protcal[8740:991452] Age Changed,current is  + -- One- -  -: -:23.826l15protcal[8740:991452] Jinpangpang -- One- -  -: -:23.827l15protcal[8740:991452] +

Objective-c Delegate

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.