The simple application of agent in OC

Source: Internet
Author: User
Tags uikit

Today and thought the old classmate chatted some, deep has the sentiment, see it passes the value all is writes with the agent, oneself usually is uses the block, the notice and so on, basically does not have the agent, thinks that the agent, feels oneself also some ambiguous, therefore has written a proxy simple application the demo, has written after thought a few, In the block and notice to do some comparison, enlightened, feel that they have a lot of help in the future.

My consistent attitude, the idea of doing the project originates from the foundation, I do not want to ask myself what will, only require their own learning can basically master the principle, so as to reach the heart, hand to, and freely.

About the official interpretation of the agent, I do not say, do not understand, I go to check some documents, here only to write its principles and usage.

Proxy is not a class, just a method of declaring a file, first look at its creation method

File:///Users/mac/Desktop/01.png

File:///Users/mac/Desktop/02.png

In the proxy file

  Simple application of the viewdelegate.h//agent////  Created by Mac on 16/3/9.//  copyright©2016 year WYP. All rights reserved.//#import <Foundation/Foundation.h> @protocol viewdelegate <nsobject>-(void) creatviewinview;-(void) AddSub: (NSString *) str;-(int) upsomething: (cgfloat) x; @end

The party that is entrusted, signs the agent, realizes the proxy method

  Simple application of the viewcontroller.m//agent////  Created by Mac on 16/3/9.//  copyright©2016 year WYP. All rights reserved.//#import "ViewController.h" #import "SViewController.h" @interface Viewcontroller () < viewdelegate>//Class Signing agreement @end@implementation viewcontroller-(void) viewdidload {    [super viewdidload];        Sviewcontroller *view = [[Sviewcontroller alloc] init]; #warning set proxy object    view. Viewdelegate = self;        } #pragma mark viewdelegate-(int) upsomething: (cgfloat) x{        NSLog (@ "x:%.1f", x);    return 15;} -(void) AddSub: (NSString *) str{        NSLog (@ "str:%@", str);} -(void) creatviewinview{        NSLog (@ "created"); @end

Principal, the party calling the proxy method

  Simple application of the sviewcontroller.h//agent////  Created by Mac on 16/3/9.//  copyright©2016 year WYP. All rights reserved.//#import <UIKit/UIKit.h> #import "ViewDelegate.h"//Introduction protocol @interface Sviewcontroller: uiviewcontroller//sets the protocol properties that determine when the Proxy method executes @property (nonatomic, weak) id<viewdelegate> viewdelegate; @end

Simple application of the sviewcontroller.m//agent////Created by Mac on 16/3/9.//copyright©2016 year WYP. All rights reserved.//#import "SViewController.h" @interface Sviewcontroller () @end @implementation sviewcontroller-( ID) init{if (self = [super init]) {Dispatch_after (Dispatch_time (Dispatch_time_now, (int64_t) (5 * nsec_p                                ER_SEC)), Dispatch_get_main_queue (), ^{//determine if the proxy object does not exist, do not execute the proxy method if (!_viewdelegate) {            return; #pragma mark invokes the method in the proxy//the proxy method with no arguments and no return value if ([_viewdelegate respondstoselector: @selector (Creatviewinvi                            EW)] {//external judgment, if the proxy object responds to this proxy method, let it execute this proxy method [_viewdelegate Creatviewinview]; }//A proxy method with no return value for the parameter if ([_viewdelegate respondstoselector: @selector (addobject:)]            ) {[_viewdelegate addsub:@ "Zhang"]; The}//has parameters that return a worthy proxy method if ([_viewdelegate RespondstosElector: @selector (upsomething:)]) {int i = [_viewdelegate upsomething:34.5];                            NSLog (@ "i=%d", I);    }                                }); } return self;} -(void) viewdidload {[Super viewdidload];}  -(void) Setviewdelegate: (id<viewdelegate>) viewdelegate{_viewdelegate = viewdelegate; } @end

The simple application of agent in OC

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.