Wtdelegate
#import <Foundation/Foundation.h>@protocol wtdelegate <NSObject>@required- (void) sleep; -(void) eat; -(void) work; @end
WTPhilosopher.h
#import<Foundation/Foundation.h>#import "WTDelegate.h"@interfacewtphilosopher:nsobject{Nstimer*timer; intcount;} @property (Nonatomic,weak)ID<WTDelegate>Delegate;- (void) Start;- (void) handle;@end
Wtphilosopher.m
#import "WTPhilosopher.h"@implementationWtphilosopher@synthesize Delegate;- (void) start{Count=0; Timer= [Nstimer scheduledtimerwithtimeinterval:3.0target:self selector: @selector (handle) Userinfo:nil Repeats:yes];}- (void) handle{Switch(count) { Case 0: [Self.Delegatesleep]; Count++; Break; Case 1: [Self.DelegateEat]; Count++; Break; Case 2: [Self.DelegateWork ]; [Timer invalidate]; Break; default: Break; }}@end
Wtviewcontroller.m
#import "WTViewController.h"#import "WTPhilosopher.h"@interfaceWtviewcontroller ()@end@implementationWtviewcontroller- (void) viewdidload{[Super Viewdidload]; Wtphilosopher*philosopher =[[Wtphilosopher alloc] init]; Philosopher.Delegate=Self ; [Philosopher start];}- (void) didreceivememorywarning{[Super didreceivememorywarning]; //Dispose of any resources the can be recreated.}#pragma--Wtdelegate Method implementation-(void) sleep{NSLog (@"sleep ...");}-(void) eat{NSLog (@"Eat ...");}-(void) work{NSLog (@"Work ...");}@end