8. Protocol Delegate proxy mode-OC

Source: Internet
Author: User

Many proxy protocols are used in iphone development and are useful in almost every project.

How to perform data interaction between the proxy and the agent.

(1) specific protocol usage

(2) How to Implement proxy

(3) how to communicate with proxy 2


The event method is implemented by humans; the dog stores human classes;

Report the information to the master;

Timer: report every 1 second to a dog. (Send data regularly when initializing a dog)

. M

Timer = [nst1_scheduledtimerwithtimeinterval: 1.0f target: selfselector:

@ Selector (updataTimer :) userInfo: nilrepeats:YES];

Meaning:[Self updateTimer: nil];

-(Void) updataTimer :( id) arg

{

BarkCount ++;

// Notify the dog owner

[Delegatebark: self count: barkCount];

}

// This function is in the population

-(Void) bark :( Dog *) thisDog count :( int) count

{


}

Do not let the main function Exit:

While (1)

{

[[Nsunloopcurrentrunloop] run];

}


Dog owner:

Id delegate; // ownner of the dog

The host id is stored when a user calls the setDog method.


Define a protocol for communication between a person and a dog:

Define a protocol in a dog

@ Protocol DogBark

-(Void) bark :( Dog *) thisDog Count :( int) count;

@ End

Id Delegate; // ownner dog's master


Implement this protocol among people


Dog Declaration and Agreement statement:

@ Class Dog; // indicates the Forward Declaration

@ Protocol DogBark; // protocol Forward Declaration



(1) specific protocol usage [event notification; called in c VOICE: callback pointer]

(2) How to Implement proxy

(3) how to communicate with proxy 2


Define Protocol

//// Dog. h // DogAndPerson // Created by ccy on 13-12-21. // Copyright (c) 2013 ccy. all rights reserved. // # import
 
  
# Import "DogToPerson. h" @ interface Dog: NSObject
  
   
{Int _ dogID; id _ delegate; // int barkCount; NSTimer * timer;} @ property int dogID; @ property (retain) id delegate; @ end
  
 

Dog call protocol:

//// DogToPerson. h // DogAndPerson // Created by ccy on 13-12-21. // Copyright (c) 2013 ccy. all rights reserved. // # import
 
  
# Import "Dog. h" @ class Dog; // remember to declare @ protocol DogToPerson
  
   
@ Optional-(void) dogBark :( Dog *) thisDog barkCount :( int) barkCount; @ end
  
 

//// Dog. m // DogAndPerson // Created by ccy on 13-12-21. // Copyright (c) 2013 ccy. all rights reserved. // # import "Dog. h "@ implementation Dog @ synthesize dogID = _ dogID; @ synthesize delegate = _ delegate;-(id) init {self = [super init]; if (self) {self. dogID = 0; barkCount = 0; timer = [NSTimer scheduledTimerWithTimeInterval: 2.0f target: self selector: @ selector (hand :) userInfo: nil repeats: YES];} return self ;} -(void) hand :( id) arg {NSLog (@ "dog hand \ n"); barkCount ++; [self. delegate dogBark: self barkCount: barkCount] ;}@ end

Person statement protocol:

/// Person. h // DogAndPerson // Created by ccy on 13-12-21. // Copyright (c) 2013 ccy. all rights reserved. // # import
 
  
# Import "Dog. h" @ interface Person: NSObject
  
   
{Dog * _ dog;} @ property (retain) Dog * dog; @ end
  
 

/// Person. m // DogAndPerson // Created by ccy on 13-12-21. // Copyright (c) 2013 ccy. all rights reserved. // # import "Person. h "@ implementation Person @ synthesize dog = _ dog;-(void) dogBark :( Dog *) thisDog barkCount :( int) barkCount {NSLog (@" dogBark thisDogID: % d, barkCount: % d \ n ", thisDog. dogID, barkCount);} @ end

Main execution Program

//// Main. m // DogAndPerson // Created by ccy on 13-12-21. // Copyright (c) 2013 ccy. all rights reserved. // # import
 
  
# Import "Dog. h "# import" Person. h "int main (int argc, const char * argv []) {@ autoreleasepool {// insert code here... NSLog (@ "Hello, World! "); Person * xiaoLi = [[Person alloc] init]; Dog * dog1 = [[Dog alloc] init]; dog1.dogID = 1024; dog1.delegate = xiaoLi; xiaoLi. dog = dog1; [dog1 release]; while (1) {[[nsunloop currentRunLoop] run];} [xiaoLi release];} return 0 ;}
 
















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.