NSNotificationCenter 调用两次
NSNotificationCenter calling two times
下面是我的。
MainViewController.m
- (IBAction)sideMenuAction:(id)sender {
NSLog(@"login==sideMenuAction");
[[NSNotificationCenter defaultCenter] postNotificationName:@"ShowMySideMenuNotification" object:self];
}
NotificationListener.m
-(void)viewDidLoad {
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"ShowMySideMenuNotification" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(adjustShowMenu) name:@"ShowMySideMenuNotification" object:nil];
}
-(void) adjustShowMenu {
NSLog(@"notification adjustShowMenu=");
}
现在,当我在 MainViewController 中单击侧边菜单按钮时,我期望的是从 NotificationListener 调用 adjustShowMenu 一次,但它被调用了两次。
下面是相同的 NSLog。
2015-01-20 12:27:30.798 abc[699:169314] login==sideMenuAction
2015-01-20 12:27:30.798 abc[699:169314] notification adjustShowMenu=
2015-01-20 12:27:30.799 abc[699:169314] notification adjustShowMenu=
我期待的是
2015-01-20 12:27:30.798 abc[699:169314] login==sideMenuAction
2015-01-20 12:27:30.798 abc[699:169314] notification adjustShowMenu=
知道出了什么问题吗?
注意:我也试过用viewDidAppear代替viewDidLoad,但结果是一样的。
网上搜索的时候,很多答案都要求removeObserver。我也这样做了,但仍然调用了两次通知。
根据answer here,我进行了如下更改,现在工作正常。
-(void) viewWillAppear:(BOOL)animated {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(adjustShowMenu) name:@"ShowMySideMenuNotification" object:nil];
}
-(void) viewWillDisappear:(BOOL)animated {
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"ShowMySideMenuNotification" object:nil];
}
下面是我的。
MainViewController.m
- (IBAction)sideMenuAction:(id)sender {
NSLog(@"login==sideMenuAction");
[[NSNotificationCenter defaultCenter] postNotificationName:@"ShowMySideMenuNotification" object:self];
}
NotificationListener.m
-(void)viewDidLoad {
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"ShowMySideMenuNotification" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(adjustShowMenu) name:@"ShowMySideMenuNotification" object:nil];
}
-(void) adjustShowMenu {
NSLog(@"notification adjustShowMenu=");
}
现在,当我在 MainViewController 中单击侧边菜单按钮时,我期望的是从 NotificationListener 调用 adjustShowMenu 一次,但它被调用了两次。
下面是相同的 NSLog。
2015-01-20 12:27:30.798 abc[699:169314] login==sideMenuAction
2015-01-20 12:27:30.798 abc[699:169314] notification adjustShowMenu=
2015-01-20 12:27:30.799 abc[699:169314] notification adjustShowMenu=
我期待的是
2015-01-20 12:27:30.798 abc[699:169314] login==sideMenuAction
2015-01-20 12:27:30.798 abc[699:169314] notification adjustShowMenu=
知道出了什么问题吗?
注意:我也试过用viewDidAppear代替viewDidLoad,但结果是一样的。
网上搜索的时候,很多答案都要求removeObserver。我也这样做了,但仍然调用了两次通知。
根据answer here,我进行了如下更改,现在工作正常。
-(void) viewWillAppear:(BOOL)animated {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(adjustShowMenu) name:@"ShowMySideMenuNotification" object:nil];
}
-(void) viewWillDisappear:(BOOL)animated {
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"ShowMySideMenuNotification" object:nil];
}