A daydream about the IOS delegate

Source: Internet
Author: User

Delegate is a very common design pattern in iOS systems and is used in many places.

Before the beginning of contact with OC, for this delegate mode is very not understand, do not know what this so-called delegate is used for.

However, after a few years of development of iOS, for this piece also has its own understanding.


First, the definition of delegate is an object that inherits a particular interface.


For example, the following interfaces are defined:

#ifndef protocol_h
#define PROTOCOL_H

@protocol testprotocol <NSObject>

-(void) firstinterface;
-(void) secondinterface;

@end


Then in another object, there is a property that inherits from this interface:

#import <Foundation/Foundation.h>
#import "Protocol.h"

@interface testclass:nsobject

@property ( Nonatomic,weak) id<testprotocol>delegate;

@end


Such benefits are like a screwdriver, which can be operated for as long as the screws that fit the screwdriver, whether long or short, because these screws have the same interface (corresponding to our protocol) and can be replaced at the right time. So delegate has a meaning for abstract programming, the object is not specific, but the interface is well defined.


Besides. There are two things delegate can do.

We often say that the delegate mode, in fact, that is the meaning. But delegate mode is not a pattern, it is two different functions.


The 1th is a callback.

A There is one thing you can not do, to find B to help deal with, after the completion of B to tell a, and then through delegate find a.

The delegate here is a, and to get the result, you have to implement the interface provided by B, which is a call to B to tell a result.


First of all, Class B. It's a tool, something that can help work, and then the object that he's working on, is a.

It is implemented as follows:

#import <Foundation/Foundation.h>

@protocol resultprotocal <NSObject>
@optional
-(void) Addresult: (nsinteger) value;
-(void) Mutiplayresult: (nsinteger) value;
@end


@interface objectb:nsobject

///calculates the sum of two numbers
-(void) Caculateadd: (Nsinteger) A value: (Nsinteger) b;
Calculates the product of two numbers
-(void) Caculatemultiply: (Nsinteger) A value: (Nsinteger) b;

@property (nonatomic,weak) id<resultprotocal>delegate;

@end


#import "ObjectB.h"


@implementation OBJECTB



///calculates the sum of two numbers
-(void) Caculateadd: (Nsinteger) a value: ( Nsinteger) b
{
    if (self.delegate && [self.delegate respondstoselector: @selector (addresult:)]) {
        [Self.delegate Addresult: (a+b)];
    }
Calculates the product of two numbers
-(void) Caculatemultiply: (Nsinteger) A value: (Nsinteger) b
{
    if (self.delegate && [ Self.delegate respondstoselector: @selector (mutiplayresult:)]) {
        [self.delegate addresult: (a*b)];
    }
}

@end

A, a just let B do the thing, but do B have to tell a ah, so a to implement the function, let b tell themselves the result:

The implementation of a is as follows:

#import "ObjectA.h"
#import "ObjectB.h"

@interface objecta () <ResultProtocal>

@end

@ Implementation Objecta




-(void) mainfunction
{

    OBJECTB *b = [OBJECTB new];//Create an object to perform the task
    b.delegate = self;//tell B you'll find me when you're done.
    [b caculateadd:100 value:200];//b begin
to perform the task}



///Object A implements the result call function of tool B completion
-( void) Addresult: (nsinteger) value
{
    
}


-(void) Mutiplayresult: (nsinteger) value
{
    
}

@ End

All right, this is the first delegate scenario, and it looks like there's a little bit of an asynchronous job that you do, and you tell me the result.

This is also an interface-oriented programming. For example, the same calculation method: Not only can have OBJECTB may also have OBJECTD or objecte, also can

Although they all provide a method of calculation, but for different scenarios, perhaps the implementation of the calculation is not the same, some can be binary, some can be recursive, some can use

More advanced algorithms and so on, with variability.


Another way to use delegate is to have a bit of a bandit's meaning. It's still a and B but, but what about the role here has to be reversed.

Of course a is still delegate but at this point a becomes the side of the work.

This is amazing. It's not magical. At this point, a integrates the method and implements the method, and B still has a delegate object, the object is a.

Look at the A code below:

#import "ObjectA.h"
#import "ObjectB.h"

@interface objecta ()

@end

@implementation objecta

-( nsstring*) Todayis
{return
 
    @ "Friday";
}
-(nsstring*) Tomorroyis
{return
    @ "Saturday";
}

@end


Let's look at B's code again:

#import <Foundation/Foundation.h>

@protocol resultprotocal <NSObject>
@optional
-( nsstring*) Todayis;
-(nsstring*) Tomorroyis;
@end


@interface objectb:nsobject

@property (nonatomic,weak) id<resultprotocal>delegate;

@end

#import "ObjectB.h"


@implementation objectb



-(void) mainfunction
{
    ///was very busy at first, and suddenly wanted to know what day it was today, so well, let a Tell me
    if (self.delegate && [self.delegate respondstoselector: @selector (Todayis)]) {
        NSString *today = [Self.delegate Todayis];
        NSLog (@ "%@", today);
    }

@end

B shake the body and say to let a person work ...

You can see from the code that the second scenario, the function implementation of the defined protocol, has a return value. This is a different place than the first one.

And this type of invocation of B is much like a synchronous operation.

A delegate two kinds of play, also very interesting.












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.