Simple implementation of IOS proxy

Source: Internet
Author: User


Yesterday did a demo, using a simple proxy.

Delegate is a design pattern for iOS programming. We can use this design pattern to let a single-inherited objective-c class exhibit the characteristics of a class outside its parent class. Yesterday this proxy implementation is as follows:

The class Gifview is inherited from UIView, and it loads on Rootviewcontroller to play the animation through a timer. At the same time, Rootviewcontroller needs to know every execution of the timer.

The code is as follows.

First, define the Gifview, define the proxy everyframedelegate in its header file, and declare the method-(void) dosomethingeveryframe;

#import <UIKit/UIKit.h>

@protocol everyframedelegate <NSObject>

-(void) dosomethingeveryframe;


Nstimer *timer;
ID <EveryFrameDelegate> delegate;
Nsinteger Currentindex;

@property (nonatomic, retain) ID <EveryFrameDelegate> delegate;


Then, just let the timer in the GIFVIEW.M call delegate to execute the Dosomethingeveryframe each time it executes, the code is as follows

-(ID) initWithFrame: (CGRect) frame

self = [super Initwithframe:frame];
if (self)
Timer = [Nstimer scheduledtimerwithtimeinterval:0.05 target:self selector: @selector (play) Userinfo:nil Repeats:yes];
[Timer fire];
return self;

-(void) play
[Delegate Dosomethingeveryframe];


The work on the Gifview is done.

The following is the code in Rootviewcontroller, Rootviewcontroller can know every execution of a timer by specifying its proxy as itself when defining Gifview:

-(void) viewdidload
[Super Viewdidload];
Additional setup after loading the view, typically from a nib.
CGRect rect = CGRectMake (0, 0, 200, 200);
Gifview *tmp = [[Gifview alloc] initwithframe:rect];
Tmp.delegate = self;
[Self.view addsubview:tmp];
[TMP release];

-(void) dosomethingeveryframe
NSLog (@ "I ' m The delegate! I ' m doing printing! ");

Every time a timer executes in a gifview, one line is printed

I ' m The delegate! I ' m doing printing!

So, Rootviewcontroller knew every time the timer was executed.

==================== I am the Happy dividing line ====================

The following content is added for 2013.3.12

In the Rootviewcontroller header file you need to introduce GifView.h this header file and indicate that Rootviewcontroller follows the proxy everyframedelegate. Otherwise, a warning will appear.

The code is as follows:

#include <UIKit/UIKit.h> #import "GifView.h" @interface Rootviewcontroller:uiviewcontroller < Everyframedelegate> @end

In addition, adding the keyword @optional when defining the proxy indicates that the agent can not implement all the proxy methods without being alerted.

The code is as follows:

@protocol everyframedelegate <NSObject> @optional-(void) ifyouneedthis;-(void) dosomethingeveryframe; @end

Simple implementation of IOS proxy

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