首页 > 代码库 > 通知---iOS
通知---iOS
#import <Foundation/Foundation.h>#define CHILD_WEAK_NOTIFC @"childe_week"@interface Child : NSObject@property (nonatomic,assign) int age;@property (nonatomic,assign) NSInteger sleep;@end
#import "Child.h"@implementation Child-(id) init{ self=[super init]; if(self!=nil){ _sleep=100; [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeAction:) userInfo:nil repeats:YES]; } return self;}-(void) timeAction:(NSTimer *) timer{ _sleep-=2; NSLog(@"%ld",_sleep); if(_sleep<90){ //发送通知 [[NSNotificationCenter defaultCenter] postNotificationName:CHILD_WEAK_NOTIFC object:[NSNumber numberWithInteger:_sleep]]; [timer invalidate]; }}@end
#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_NOTIFC object:nil]; } return self;}-(void)weakNotification:(NSNotification *) notification{ NSLog(@"sdkjflskdjf"); NSNumber *number=notification.object; NSLog(@"%@",number);}@end
Child *child=[[Child alloc]init]; Father *father=[[Father alloc]init]; NSLog(@"%@",child); [[NSRunLoop currentRunLoop] run];
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。