Ios NSNotificationCenter不工作,如何调试
我遵循了这个例子,但我似乎无法让我的通知起作用。是否有一个框架或我需要添加的东西?如何调试代码 FirstViewController发布 第二视图控制器接收Ios NSNotificationCenter不工作,如何调试,ios,objective-c,nsnotificationcenter,Ios,Objective C,Nsnotificationcenter,我遵循了这个例子,但我似乎无法让我的通知起作用。是否有一个框架或我需要添加的东西?如何调试代码 FirstViewController发布 第二视图控制器接收 移除[[NSNotificationCenter defaultCenter]移除观察者:self]添加观察者后,它将工作 或者您可以移动[[NSNotificationCenter defaultCenter]removeObserver:self]至解除锁定方法 - (void)dealloc { [[NSNotificati
移除
[[NSNotificationCenter defaultCenter]移除观察者:self]代码>添加观察者后,它将工作
或者您可以移动[[NSNotificationCenter defaultCenter]removeObserver:self]代码>至解除锁定
方法
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
关于为什么它在第一次运行时不工作的问题
这是因为在初始化SecondViewController
之前调用了postNotificationName
。要修复它,请尝试以下代码
- (IBAction)btnSend:(id)sender {
self.tabBarController.selectedIndex = 1;
[[NSNotificationCenter defaultCenter] postNotificationName:@"TestNotification"
object:self];
}
移除[[NSNotificationCenter defaultCenter]移除观察者:self]代码>添加观察者后,它将工作
或者您可以移动[[NSNotificationCenter defaultCenter]removeObserver:self]代码>至解除锁定
方法
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
关于为什么它在第一次运行时不工作的问题
这是因为在初始化SecondViewController
之前调用了postNotificationName
。要修复它,请尝试以下代码
- (IBAction)btnSend:(id)sender {
self.tabBarController.selectedIndex = 1;
[[NSNotificationCenter defaultCenter] postNotificationName:@"TestNotification"
object:self];
}
当我第一次启动应用程序并单击BTN发送时,它会转到SecondViewController,但不会触发addObserver
或receiveTestNotification:
。我必须单击第一个VC选项卡,然后再次单击btnSend
,然后所有功能都会相应地运行?如果不单击btnSend
,它怎么会触发?当应用程序完成启动后,我单击btnSend,它就不工作了。它只直接到第二个VC。然后,当我单击选项卡返回到FirstVC并再次单击btnSend时,只有它工作?这是因为在更改选项卡之前调用postNotificationName
,或者我可以说在初始化SecondViewController
之前调用了postNotificationName
。要使其工作,请像在-(iAction)btnSend:(id)sender中那样交换2method@HansheungCheah更新我的回答当我第一次启动应用程序并单击BTN发送时,它会转到SecondViewController,但不会触发addObserver
或receiveTestNotification:
。我必须单击第一个VC选项卡,然后再次单击btnSend
,然后所有功能都会相应地运行?如果不单击btnSend
,它怎么会触发?当应用程序完成启动后,我单击btnSend,它就不工作了。它只直接到第二个VC。然后,当我单击选项卡返回到FirstVC并再次单击btnSend时,只有它工作?这是因为在更改选项卡之前调用postNotificationName
,或者我可以说在初始化SecondViewController
之前调用了postNotificationName
。要使其工作,请像在-(iAction)btnSend:(id)sender中那样交换2method@HansheungCheah更新了我的答案
// 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];
}