Protocol (PROTOCOL) and principal agent (Delegate)

Source: Internet
Author: User

Protocol (Protocol) 's role:

1. Specification interface, used to define a common set of interfaces;

2. Constrain or filter the object.

Agent (Delegate):

It is in itself a design pattern that delegates an object < follows the protocol > does something to reduce the coupling between objects, or to reverse the value.

I. Define a common set of interfaces

1 /** Agreement*/2@protocol Executeprotocol <NSObject>3 4 @required5 /**6 * @brief A method that must be implemented7  */8-(Nsuinteger) qualified;9 Ten  One @optional A /** - * @brief Optional method -  */ the- (void) dosomething; -  -@end

Protocol only. h files, no. m files. Because Protocol only defines a common set of interfaces, it does not provide a concrete implementation method. (A specific implementation requires a class that adheres to the protocol, and then the class can do things as a filtered object, which is discussed later)

If the controller needs to use the protocol, then the import protocol:

1 #import "ExecuteProtocol.h"

and implement the required method of the Protocol (otherwise the compiler will warning)

The code for Viewcontroller is as follows:

1 #import "ViewController.h"2 #import "ExecuteProtocol.h"3 #import "Object.h"4 5 @interfaceViewcontroller ()6@property (nonatomic, strong) UILabel *label;7 @end8 9 @implementationViewcontrollerTen  One #pragmaMark-view lifeCycle A- (void) Viewdidload { - [Super Viewdidload]; - [Self.view AddSubview:self.label]; the [self Gethouse:[[object alloc] init]]; - } -  -- (void) Gethouse: (ID<ExecuteProtocol>) obj { +Self.label.text = [NSString stringWithFormat:@"%lu", [obj qualified]]; - } +  A #pragmaMark-getter Methods at-(UILabel *) Label { -     if(!_label) { -_label = [[UILabel alloc] Initwithframe:cgrectmake ( -, -, -, -)]; -_label.textalignment =Nstextalignmentcenter; -_label.backgroundcolor =[Uicolor Redcolor]; -     } in     return_label; - } to @end

To add a method to the controller, the parameter of this method must be an object that adheres to the protocol, so the object is created:

1 #import <Foundation/Foundation.h>2#import"ExecuteProtocol.h " 3 4 /*  */5@interface object:nsobject <ExecuteProtocol>6 7 @end

and implement the Protocol method:

1 #import " Object.h " 2 3 @implementation Object 4 5 - (Nsuinteger) qualified {6     return; 7 }89@end

Simple small Demo.

Second, the agent value (Secondaryviewcontroller value to Viewcontroller)

1. In the Viewcontroller

1 // Viewcontroller need to comply with the agent 2 @interface Viewcontroller () <SecondaryViewControllerDelegate>34  Secondaryviewcontroller *SECVC = [[Secondaryviewcontroller alloc] init]; 5  // Specify proxy 6  SECVC. delegate = self ; 7  [Self.navigationcontroller PUSHVIEWCONTROLLER:SECVC Animated:yes];

1 // implement required Proxy method, realize the value of the transfer, print the result 2 #pragma mark-secondaryviewcontrollerdelegate Methods3 -(void) Controller: ( Secondaryviewcontroller *) Controller text: (NSString *) text {4     NSLog (@ "%@%@  ", Controller, text); 5 }

2. In the Secondaryviewcontroller

1) First, declare the agent

1 #import<UIKit/UIKit.h>2 @classSecondaryviewcontroller;3 4 /**5 * @brief Protocol method (class name +delegate)6  */7 @protocolSecondaryviewcontrollerdelegate <NSObject>8 @required9 /**Ten * @brief Pass value One  * A * @param controllers Current Controller - * @param text value -  */ the- (voidController: (Secondaryviewcontroller *) controller text: (NSString *) text; - @end -  - @interfaceSecondaryviewcontroller:uiviewcontroller + /** - * @brief Agent modified with weak (to prevent memory leaks) +  */ A@property (nonatomic, weak)ID<SecondaryViewControllerDelegate>Delegate; at @end

2) Determine if the agent exists or not and whether the method responds

1 /** 2 * Secondaryviewcontroller3  */4- (void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Uievent *)Event {5     /**6 * @brief Determine if the agent is set and the agent responds to the proxy method7      */8     if(self.)Delegate&& [Self.Delegaterespondstoselector: @selector (controller:text:)]) {9[Self.DelegateController:self text:@"Pass Value"];Ten     } One [Self.navigationcontroller Popviewcontrolleranimated:yes]; A}

respecting the author's labor achievements, reprint please specify: "Kingdev"

Protocol (PROTOCOL) and principal agent (Delegate)

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.