Big talk design mode--entrusted--ios

Source: Internet
Author: User

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

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.