#import <Foundation/Foundation.h>
#import "Child.h"
#import "Father.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
Child *child = [[Child alloc]init];
Father *father = [[Father alloc]init];
[[NSRunLoop currentRunLoop] run];
}
return 0;
}
#import <Foundation/Foundation.h>
@interface Father : NSObject
@end
#import "Father.h"
#import "Child.h"
@implementation Father
- (id)init{
self = [super init];
if (self != nil) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(weakNotification:) name:CHILD_WEAK_NOTIFICATION object:nil]; //监听通知
}
return self;
}
//监听事件触发
- (void)weakNotification:(NSNotification *)notification {
NSNumber *num = notification.object;
NSLog(@"抱起宝宝哄哄!_sleep = %@",num);
}
@end
#import <Foundation/Foundation.h>
#define CHILD_WEAK_NOTIFICATION @"child_weak"
@interface Child : NSObject
@property (nonatomic,assign)NSInteger sleep;
@end
#import "Child.h"
@implementation Child
- (id)init{
self = [super init];
if (self != nil) {
self.sleep = 100;
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
}
return self;
}
- (void)timerAction:(NSTimer *)timer {
self.sleep -=2;
NSLog(@"%ld",_sleep);
if (_sleep < 90) {
[[NSNotificationCenter defaultCenter] postNotificationName:CHILD_WEAK_NOTIFICATION object:[NSNumber numberWithInteger:_sleep]]; //通知发出
[timer invalidate];
}
}
@end