【发布时间】:2016-08-18 09:29:02
【问题描述】:
场景我有两个视图控制器:Tabcontroller 和 Viewcontroller。在第一个中,我定义了我的发布通知,在第二个中,我添加了一个带有接收方法的观察者。
在我的 tabcontroller(发件人)上:
-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
[[NSNotificationCenter defaultCenter] postNotificationName:@"RestartBtn" object:self userInfo:@{@"isHidden": @"YES"}];
}
在视图控制器(接收器)上:
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"HallOfFameView");
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveNotification:) name:@"RestartBtn" object:nil];
}
-(void)receiveNotification:(NSNotification*)notification {
NSLog(@"%@",[notification name]);
_restartbutton.hidden=YES;
NSLog(@"%@",notification.userInfo[@"isHidden"]);
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter removeObserver:self name:@"RestartBtn" object:nil];
}
当我点击我的 tabbaritem 时,它会推送一个视图控制器,它会打印“HallOfFameView”,我相信这会注册观察者,但不会在第一次执行接收通知方法,只是在第二次。例如,当我单击标签栏项目(“主页”)时,它将执行接收通知方法。
为什么第一次没有工作?以及为什么它已经从我期望工作的视图控制器退出(这是我添加观察者的地方)。
【问题讨论】:
-
以下我的回答有效吗?
标签: ios objective-c nsnotificationcenter nsnotifications