For IOS developers, use the Protocol and the Delegate agent (Delegate) to pass the value, and use the protocoldelegate

Source: Internet
Author: User

For IOS developers, use the Protocol and the Delegate agent (Delegate) to pass the value, and use the protocoldelegate

Because Object-C does not support multi-inheritance, it is often replaced by Protocol. Protocol can define only one set of public interfaces, but it cannot provide specific implementation methods. That is to say, it only tells you what to do, but does not care about how to do it.

 

When a class uses a Protocol (Protocol), it must comply with the Protocol. For example, if you do not implement some necessary implementation methods, the compiler will report a warning to remind you that you have not followed the ×× protocol. Note: Here I am talking about warnings, not errors. Yes, even if you don't implement the "necessary implementation" methods, the program can run, but there are some additional warnings.

 

I will put this Demo at the end of this Article. If you need it, you can download it. Thank you.

 

Role of Protocol:

1. define a set of Public interfaces (Public)

@ Required: required Method

@ Optional: optional implementation method (not all can be implemented)

2. Delegate proxy (Delegate) to pass the value:

  It is a design pattern, which means entrusting others to do something.

For example, Class A calls the method of class B and class B encounters A problem during execution. In this case, we need to use A proxy (Delegate ).

Another example is the transfer of values between the Controller and the Controller, which redirects from C1 to C2 and then notifies C1 to update the UI or do other things when returning from C2 to C1, at this time, we will use the proxy (Delegate) to pass the value.

 

1. define a set of Public interfaces (Public)

Create a new protocol file:

 

 

Fill in the Protocol File Name and file type (select Protocol ):

 

ProtocolDelegate. h code (the Protocol does not generate the. m file ):

# Import <Foundation/Foundation. h> @ protocol ProtocolDelegate <NSObject> // required method @ required-(void) error; // optional implementation method @ optional-(void) other;-(void) other2;-(void) other3; @ end

 

 

When you need to use the Protocol Class, import its header file:

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

 

Here I select the entry file

 

Remember to abide by the agreement:

@interface ViewController () <ProtocolDelegate>@end

 

 

At this time, a warning will be reported, because one of the defined protocols is a required method, and we have not implemented it:

 

 

After implementation of the required method, the compiler will not report a warning:

For other optional methods, you can choose to implement or not implement them all.

 

2. value transfer by Delegate (Delegate)

On the Storyboard, set up the interface first, for example:

 

Create ControllerB:

 

Set the class on the B interface to ViewControllerB:

 

The main file-like code is released below. I have written comments in it, so you should be able to understand them. It doesn't matter if you don't understand it. I will put a Demo at the end of this article.

ViewController. m file:

# Import "ViewController. h "# import" ProtocolDelegate. h "# import" ViewControllerB. h "@ interface ViewController () <ProtocolDelegate, scheme> @ end @ implementation ViewController-(void) prepareForSegue :( UIStoryboardSegue *) segue sender :( id) sender {ViewControllerB * vc = segue. destinationViewController; [vc setDelegate: self];} // here the Protocol method for implementing controller B-(void) sendValue :( NSString *) value {UIAlertView * alertView = [[UIAlertView alloc] initWithTitle: @ "success" message: value delegate: nil cancelButtonTitle: @ "OK" otherButtonTitles: nil, nil]; [alertView show];}-(void) error {}@ end

 

 

ViewControllerB. h file:

# Import <UIKit/UIKit. h> // create a new protocol. The protocol name is generally set to "class name + Delegate" @ protocol ViewControllerBDelegate <NSObject> // The method for passing values by proxy-(void) sendValue :( NSString *) value; @ end @ interface ViewControllerB: UIViewController // delegate agent. Generally, weak reference (weak) @ property (weak, nonatomic) is used as the proxy) id <ViewControllerBDelegate> 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. navigationController popViewControllerAnimated: YES];} @ end

 

Effect:

 

 

Summary:

When you need to define a set of public interfaces and the implementation methods can be different, you can use the Protocol.

When you need to transfer values between classes, you can also use the proxy design mode to transfer values based on the Protocol.

 

 

Demo testing environment:

Development Tool: Xcode6.1 testing model: simulator Testing System: IOS8.0

Demo: GCProtocol & Delegate

 

 

 

Blog Author: GarveyCalvin

Blog Source: http://www.cnblogs.com/GarveyCalvin/

The copyright of this article is shared by the author and the blog. You are welcome to repost it, but you must keep this statement and provide the original article link. Thank you for your cooperation!

 

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.