首页 > 代码库 > 通知的使用
通知的使用
Main.m
#import "Children.h" #import "Nurse.h" int main(int argc, const char * argv[]) { @autoreleasepool { Children *children = [[Children alloc] init]; Nurse *nurse = [[Nurse alloc] initWithChildren:children]; [[NSRunLoop currentRunLoop] run]; [children release]; [nurse release]; } return 0; }
Children.h
@interface Children : NSObject @property(nonatomic,assign)NSInteger happyValue; //欢乐值 @property(nonatomic,assign)NSInteger hungryValue; //饥饿值
Children.m
- (id)init { self = [super init]; if (self) { //开启定时器 [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeAction:) userInfo:nil repeats:YES]; _hungryValue = http://www.mamicode.com/100;>
Nurse.h@class Children; @interface Nurse : NSObject { Children *_children; } - (id)initWithChildren:(Children *)children;
Nurse.m#import "Children.h" @implementation Nurse - (id)initWithChildren:(Children *)children { self = [super init]; if (self) { _children = [children retain]; //接收通知,通知名:happlyValueNotification [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(happlyValueChange) name:@"happlyValueNotification" object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hungryValueChange:) name:@"hungryValueNotification" object:nil]; } return self; } - (void)happlyValueChange { [self play]; } - (void)hungryValueChange:(NSNotification *)notification { [self feed]; } - (void)play { NSLog(@"保姆陪小孩玩耍"); _children.happyValue = http://www.mamicode.com/100;>通知的使用
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。