NSNotificationCenter 不工作,如何调试
NSNotificationCenter not working, how to debug
我遵循了 dreammlax 示例,但我似乎无法让我的 NSNotification 工作。是否有框架或我需要添加的东西?我如何调试代码。
FirstViewController 发布
#import "FirstViewController.h"
@interface FirstViewController ()
@end
@implementation FirstViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (IBAction)btnSend:(id)sender {
[[NSNotificationCenter defaultCenter] postNotificationName:@"TestNotification"
object:self];
self.tabBarController.selectedIndex = 1;
}
@end
SecondViewController 接收
#import "SecondViewController.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(receiveTestNotification:)
name:@"TestNotification"
object:nil];
//===Removed ===[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void) receiveTestNotification:(NSNotification *) notification
{
if ([[notification name] isEqualToString:@"TestNotification"])
NSLog (@"Successfully received the test notification!");
}
@end
添加观察者后删除[[NSNotificationCenter defaultCenter] removeObserver:self];
即可
或者您可以将 [[NSNotificationCenter defaultCenter] removeObserver:self];
移动到 dealloc
方法
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
关于为什么第一次不行的问题运行。
因为在SecondViewController
初始化之前调用了postNotificationName
。要修复它,请尝试以下代码。
- (IBAction)btnSend:(id)sender {
self.tabBarController.selectedIndex = 1;
[[NSNotificationCenter defaultCenter] postNotificationName:@"TestNotification"
object:self];
}
// from where u want to post notification
- (IBAction)PressedMeBtn:(id)sender {
KNcreateOfferBack is define string!!!!
[[NSNotificationCenter defaultCenter] postNotificationName:KNcreateOfferBack object:nil];
}
//And where you are sending notification and observer
- (void)BackFromControllers:(NSNotification *)note {
NSLog(@"Received Notification Inside LeftMenu - event");
if([[note name] isEqualToString:KNcreateOfferBack]){
NSLog(@"KNcreateOfferBack NoteName :***: %@ :***:",note.name);
// done your work here and then remove your notification !!!!
[[NSNotificationCenter defaultCenter] removeObserver:self name:KNcreateOfferBack object:nil];
}
// use observer in viewWillAppear where you are using above method
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(BackFromControllers:)
name:KNcreateOfferBack object:nil];
}
我遵循了 dreammlax 示例,但我似乎无法让我的 NSNotification 工作。是否有框架或我需要添加的东西?我如何调试代码。
FirstViewController 发布
#import "FirstViewController.h"
@interface FirstViewController ()
@end
@implementation FirstViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (IBAction)btnSend:(id)sender {
[[NSNotificationCenter defaultCenter] postNotificationName:@"TestNotification"
object:self];
self.tabBarController.selectedIndex = 1;
}
@end
SecondViewController 接收
#import "SecondViewController.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(receiveTestNotification:)
name:@"TestNotification"
object:nil];
//===Removed ===[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void) receiveTestNotification:(NSNotification *) notification
{
if ([[notification name] isEqualToString:@"TestNotification"])
NSLog (@"Successfully received the test notification!");
}
@end
添加观察者后删除[[NSNotificationCenter defaultCenter] removeObserver:self];
即可
或者您可以将 [[NSNotificationCenter defaultCenter] removeObserver:self];
移动到 dealloc
方法
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
关于为什么第一次不行的问题运行。
因为在SecondViewController
初始化之前调用了postNotificationName
。要修复它,请尝试以下代码。
- (IBAction)btnSend:(id)sender {
self.tabBarController.selectedIndex = 1;
[[NSNotificationCenter defaultCenter] postNotificationName:@"TestNotification"
object:self];
}
// from where u want to post notification
- (IBAction)PressedMeBtn:(id)sender {
KNcreateOfferBack is define string!!!!
[[NSNotificationCenter defaultCenter] postNotificationName:KNcreateOfferBack object:nil];
}
//And where you are sending notification and observer
- (void)BackFromControllers:(NSNotification *)note {
NSLog(@"Received Notification Inside LeftMenu - event");
if([[note name] isEqualToString:KNcreateOfferBack]){
NSLog(@"KNcreateOfferBack NoteName :***: %@ :***:",note.name);
// done your work here and then remove your notification !!!!
[[NSNotificationCenter defaultCenter] removeObserver:self name:KNcreateOfferBack object:nil];
}
// use observer in viewWillAppear where you are using above method
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(BackFromControllers:)
name:KNcreateOfferBack object:nil];
}