//
// main.m
// 委托(代理)练习
//
// Created by on 14-10-8.
// Copyright (c) 2014年 apple. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Morther.h"
#import "Nurse.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
Nurse *nurse = [[Nurse alloc] init];
Morther *Sona = [[Morther alloc] initWithName:@"Sona" withDelegate:nurse];
[NSTimer scheduledTimerWithTimeInterval:2 target:Sona selector:@selector(delegateThings) userInfo:@"SonaInfo" repeats:YES];
[[NSRunLoop currentRunLoop] run];
}
return 0;
}
//
// Morther.h
// 委托(代理)练习
//
// Created by on 14-10-8.
// Copyright (c) 2014年 apple. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Job.h"
@interface Morther : NSObject
@property (nonatomic, copy) NSString *name;
@property (nonatomic, assign) id <Job> delegate;
- (id) initWithName:(NSString *) _name withDelegate:(id <Job>)_delegate;
- (void) delegateThings;
@end
//
// Morther.m
// 委托(代理)练习
//
// Created by on 14-10-8.
// Copyright (c) 2014年 apple. All rights reserved.
//
#import "Morther.h"
@implementation Morther
@synthesize name;
@synthesize delegate;
- (id) initWithName:(NSString *) _name withDelegate:(id <Job>)_delegate {
if (self = [super init]) {
self.name = _name;
self.delegate = _delegate;
}
return self;
}
- (void) delegateThings {
[delegate takePlay];
}
@end
//
// Nurse.h
// 委托(代理)练习
//
// Created by on 14-10-8.
// Copyright (c) 2014年 apple. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Job.h"
@interface Nurse : NSObject <Job>
@end
//
// Nurse.m
// 委托(代理)练习
//
// Created by on 14-10-8.
// Copyright (c) 2014年 apple. All rights reserved.
//
#import "Nurse.h"
@implementation Nurse
-(void)takePlay {
NSLog(@"护士的工作是陪孩子玩耍。");
}
@end
//
// Job.h
// 委托(代理)练习
//
// Created by on 14-10-8.
// Copyright (c) 2014年 apple. All rights reserved.
//
@protocol Job <NSObject>
-(void)takePlay;
@end