iOS Development-protocol Protocol and principal agent (Delegate) Transfer value

Source: Internet
Author: User

Preface: Because OBJECT-C does not support multiple inheritance, it is often replaced with protocol (protocol). The Protocol (protocol) can only define a common set of interfaces, but cannot provide a specific implementation method. That is, it only tells you what to do, but specifically how to do it, it does not care.

When a class is going to use a certain protocol (protocol), it must obey the protocol. For example, there are some necessary ways to implement, you do not implement, then the compiler will report a warning to remind you not to abide by the XX protocol. Note that I'm talking about a warning, not a mistake. Yes, even if you do not implement the "need to achieve" method, the program can also run, but a few more warnings.

I will put this demo at the end of this article, if necessary, you can download, thank you.

The role of Protocol (protocol):

1. Define a common set of interfaces (public)

    • @required: Methods that must be implemented

    • @optional: Optional method of implementation (none possible)

2. Principal agent (Delegate) value:

It is a design pattern in itself, it means entrusting someone else to do something.

For example: The value of two classes, Class A called the method of Class B, class B encountered a problem in the execution of the notification Class A, this time we need to use the agent (Delegate).

Another example: the controller and Controller (controllers) between the value of the transfer, from C1 to C2, and then return from C2 to C1 when you need to notify C1 update UI or do other things, this time we use the proxy (Delegate) value.

I. Define a common set of interfaces (public)

First create a new protocol file:

Fill in the protocol file name and file type (select protocol):

ProtocolDelegate.h Code (Protocol does not generate. m files):

#import @protocol protocoldelegate//The method that must be implemented @required-(void) error;//the optional Implementation method @optional-(void) other;-(void) other2;-( void) Other3; @end

In the class that needs to use the protocol, import its header file:

#import "ViewController.h" #import "ProtocolDelegate.h"

I chose the entry file here.

Remember to abide by the agreement:

@interface Viewcontroller () @end

A warning is reported because one of the defined protocols is a method that must be implemented, and we do not implement:

After implementing a method that must be implemented, the compiler does not report a warning:

As with other alternative methods, you can either choose to implement them, or none of them can be implemented.

Second, the principal agent (Delegate) transmission value

On the storyboard, first make a good interface, such as:

New Controllerb:

Set the class of the B interface to Viewcontrollerb:

The following release of the main class file code, I wrote a note inside, you should be able to read. I will put the demo at the end of this article if I don't understand it.

VIEWCONTROLLER.M file:

#import "ViewController.h" #import "ProtocolDelegate.h" #import "ViewControllerB.h" @interface Viewcontroller () @  End@implementation viewcontroller-(void) Prepareforsegue: (Uistoryboardsegue *) Segue Sender: (ID) sender{  Viewcontrollerb *VC = Segue.destinationviewcontroller; [VC setdelegate:self];} Here implement the Protocol method of the B controller-(void) Sendvalue: (NSString *) value{Uialertview *alertview = [[Uialertview alloc] initwithtitle:@ "Success" M  Essage:value delegate:nil cancelbuttontitle:@ "OK" otherbuttontitles:nil, nil]; [Alertview show];} -(void) error{} @end

ViewControllerB.h file:

#import//Create a new protocol in which the name of the protocol is generally "class name +delegate" @protocol viewcontrollerbdelegate//Proxy value Method-(void) Sendvalue: (NSString *) value; @end @interface viewcontrollerb:uiviewcontroller//delegate, the agent generally uses a weak reference (weak) @property (weak, nonatomic) ID delegate; @end

VIEWCONTROLLERB.M file:

#import "ViewControllerB.h" @interface Viewcontrollerb () @property (Strong, nonatomic) Iboutlet Uitextfield *textfield; @end @implementation viewcontrollerb-(ibaction) Backaction: (ID) sender{if ([_delegate respondstoselector: @selector ( Sendvalue:)] {//If the protocol responds to Sendvalue: method [_delegate Sendvalue:_textfield.text];//Notification execution protocol method} [Self.navigationcontrolle R Popviewcontrolleranimated:yes];} @end

Finish the effect:

Summary:

When you need to define a common set of interfaces, the implementation method can be different when you can use the Protocol protocol.

When you need to pass values between classes and classes, you can also use the proxy design pattern to pass values based on the Protocol protocol.

Demo Test through the environment:

    • Development tools: Xcode6.1

    • Test Model: Simulator

    • Test system: IOS8.0

Demo: gcprotocol&delegate

iOS Development-protocol Protocol and principal agent (Delegate) Transfer value

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.