Recently read some about the iOS commissioned articles, after reading, feel not good.
Citation:
Delegate delegation is a simple but powerful design pattern that functions as one object in a program represents another object, or an object works with another object. The delegate object retains a reference to another object, that is, the delegate, and the message is sent to the delegate at the appropriate time. The delegate method for the message notification event that the delegate object is going to handle or has already processed. The delegate may respond to the message by updating the State or appearance of itself or other objects in the application, and in some cases the delegate may return a value, thereby affecting how the event will be handled. The main value of the delegate pattern is to allow you to simply customize the behavior of certain objects through a central object.
This description is obviously for someone who already knows.
I believe that the novice will not understand after looking at it.
So, let me summarize and see if it's helpful to everyone:
A delegate involves three parts: the Protocol (delegate), the class of the delegate, the class that implements the delegate.
1. Agreement, not much explanation, for example:
@protocol myviewdelegate <nsobject>-(void) Textenter: (nsstring*) text; @end
2. Class of the delegate
Suppose the class is Shengmingviewcontroller
Declare a property (for external invocation), which follows the above protocol:
@property (Assign, nonatomic) id<myviewdelegate> mydelegate;
Re-invoke the method in the protocol:
The method in triggering the protocol in a Click event (ibaction) savebuttonclicked: (ID) Sender { //whether there is an implemented object if ([MyDelegate Respondstoselector: @selector (textentered:)]) { //If there is, invoke its implementation [MyDelegate textentered: Textentered.text]; } Interface jump, with Agent no yarn relationship [self dismissmodalviewcontrolleranimated:yes];}
The above code and comments are clear and not much explained.
3. Classes that implement delegates
Follow the agreement
@interface shixianviewcontroller:uiviewcontroller<myviewdelegate>
Delegate's Class object
Shengmingviewcontroller *VC = [[Shengmingviewcontroller alloc] init]; Vc.mydelegete = self;
Implementing the methods in the Protocol
-(void) textentered: (NSString *) text{ }
Big talk design mode--entrusted--ios