通知的一个注意事项

关于通知的知识网上已有不少介绍,这里就不不做多余的说明了,想提醒的就是

在你发送通知的时候,一定要先有监听者监听才可以,否则将会导致第一次发出的通知无法监听的问题

#import#import "ViewController.h"

@interface Alvin : NSString

@end

@interface Alvin (one)

+ (void)PutOne;

@end

@interface Alvin (two)

+ (void)PutTwo;

@end

#import "Alvin.h"

@implementation Alvin

- (void)dealloc

{

[super dealloc];

[[NSNotificationCenter defaultCenter] removeObserver:self];

}

@end

#import "Alvin.h"

@implementation Alvin (one)

+ (void)PutOne

{

NSLog(@"one");

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(btnone) name:JUMPSUCCESS object:nil];

}

+ (void)btnone

{

NSLog(@"ALvin one");

}

@end

#import "Alvin.h"

@implementation Alvin (two)

+ (void)PutTwo

{

NSLog(@"two");

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(btntwo) name:JUMPSUCCESS object:nil];

}

+ (void)btntwo

{

NSLog(@"ALvin two");

}

@end

推荐阅读更多精彩内容