FirstController.m
- (IBAction)done:(id)sender {
NSNotification *msg = [NSNotification notificationWithName:@"addNevItem" object:[NSString stringWithFormat:@"%i",1]];
[[NSNotificationCenter defaultCenter] postNotification:msg];
}TwoController.m
- (void)viewDidLoad
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(addNevItem:)
name:@"addNevItem"
object:nil];
}
-(void)addNevItem:(NSNotification *)notification {
NSLog(@"dd");
}如果只执行一次操作,我会在控制台中看到一条消息。如果该操作执行了两次,那么在控制台中我还会看到另外两次。如果该操作执行了三次,那么在控制台中我还会看到另外三次。为什么会发生这种情况?我在程序的其他部分使用相同的代码,总是只有一条消息。
发布于 2013-12-22 08:48:04
您每次执行操作时都会发布一个通知,所以很自然地,您就会收到同样多的通知。
但
您已经忘记(或未显示;)调用removeObserver,以便通知可以“堆积”(每个活着的VC都得到通知)。
https://stackoverflow.com/questions/20727399
复制相似问题