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):

1234567891011 #import @protocol ProtocolDelegate // 必须实现的方法@required- (void)error;// 可选实现的方法@optional- (void)other;- (void)other2;- (void)other3;@end

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

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

I chose the entry file here.

Remember to abide by the agreement:

1 @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:

1234567891011121314151617181920 #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];}// 这里实现B控制器的协议方法- (void)sendValue:(NSString *)value{  UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"成功"message:value delegate:nil cancelButtonTitle:@"确定"otherButtonTitles:nil, nil];  [alertView show];}- (void)error{}@end

ViewControllerB.h file:

123456789101112 #import // 新建一个协议,协议的名字一般是由“类名+Delegate”@protocol ViewControllerBDelegate // 代理传值方法- (void)sendValue:(NSString *)value;@end @interface ViewControllerB : UIViewController // 委托代理人,代理一般需使用弱引用(weak)@property (weak, nonatomic) id delegate;@end

VIEWCONTROLLERB.M file:

12345678910111213 #import "ViewControllerB.h"@interface ViewControllerB ()@property (strong, nonatomic) IBOutlet UITextField *textField;@end@implementation ViewControllerB- (IBAction)backAction:(id)sender{  if([_delegate respondsToSelector:@selector(sendValue:)]) { // 如果协议响应了sendValue:方法    [_delegate sendValue:_textField.text]; // 通知执行协议方法  }  [self.navigationController 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

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.