iOS Development-Protocol Protocol and Delegate Value Transfer

Keywords: xcode simulator

Preface: Because Object-C does not support multi-inheritance, it is often replaced by Protocol. Protocol can only define a common set of interfaces, but it can not provide specific implementation methods. That is to say, it only tells you what to do, but it doesn't care what to do.

When a class wants to use a Protocol, it must abide by the protocol. For example, if you don't implement some necessary methods, the compiler will alarm you to remind you that you haven't complied with the * protocol. Note that what I'm talking about here is warning, not error. Yes, even if you don't implement the "necessary implementation" approach, the program will work, with just a few more warnings.

I will put the download address of Demo at the end of this article. If necessary, you can download it. Thank you.

The role of Protocol:

Define a common set of interfaces (Public)

@ Required: The methods that must be implemented are required by default in @protocol.
@ optional: Optional implementations (none of them can be implemented)

2. Delegate transfer value:

It's a design pattern in itself. It means to entrust someone to do something.
For example, class A calls class B's methods, class B notifies class A of problems in execution, and we need to use a Delegate.
For example, we need to notify C1 to update UI or do other things when we return from C2 to C1, when the value between Controller and Controller jumps from C1 to C2, then we use Delegate to transfer value.

Define a common set of interfaces (Public)

First, create a new protocol file:


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 <Foundation/Foundation.h>

@protocol ProtocolDelegate <NSObject>

// Methods that must be implemented
@required
- (void)error;

// Optional implementations
@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 () <ProtocolDelegate>

@end

A warning will be issued because there is a method in the defined protocol that must be implemented, and we have not implemented it:


There is a method that must be implemented, so warning

No warning after implementing this method


As for other alternatives, you can choose to implement them or not at all.

2. Delegate Value Transfer

On Storyboard, build the interface first, as follows:


Interface


New Controller B:


New Controller B


Set the class of the B interface to ViewController B:


Set the class of the B interface to ViewController B

Below is the main class file code, I wrote a comment in it, you should be able to understand. It doesn't matter if I don't understand. I'll put Demo download address at the end of this article.

ViewController.m Documents:
#import "ViewController.h"
#import "ProtocolDelegate.h"
#import "ViewControllerB.h"

@interface ViewController () <ProtocolDelegate, ViewControllerBDelegate>

@end

@implementation ViewController

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    ViewControllerB *vc = segue.destinationViewController;
    [vc setDelegate:self];
}

// The Protocol Method of Implementing B Controller
- (void)sendValue:(NSString *)value
{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Success" message:value delegate:nil cancelButtonTitle:@"Determine" otherButtonTitles:nil, nil];
    [alertView show];
}

- (void)error
{

}


@end

ViewControllerB.h Documents:
#import <UIKit/UIKit.h>

// Create a new protocol whose name is usually "class name + Delegate"
@protocol ViewControllerBDelegate <NSObject>

// Proxy Value Passing Method
- (void)sendValue:(NSString *)value;

@end

@interface ViewControllerB : UIViewController

// Principal agent, agent generally need to use weak reference.
@property (weak, nonatomic) id<ViewControllerBDelegate> delegate;

@end

ViewControllerB.m Documents:
#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

Complete the effect screenshot:


Completion effect

Summary:
When you need to define a common set of interfaces that can be implemented differently, you can use the Protocol protocol.
When you need to pass values between classes, you can also use proxy design pattern to pass values based on Protocol protocol.

Demotest Through the environment:
Development tools: Xcode 6.1 test machine: simulator test system: IOS8.0

Demo Download Address: GCProtocol&Delegate
Looking at Baidu and searching for this article, I found that some links to reprinted websites were lost, so I added one here:
Demo Download Address:: http://pan.baidu.com/s/1ntJYgvJ

Posted by AbeFroman on Sun, 07 Jul 2019 16:34:05 -0700