NSNotificationCenter 在弹出视图中不起作用(ios objC)
NSNotificationCenter doesn't work in popup view(ios objC)
我正在做一个 iPad 项目(xcode 7.21+iOS9)但 NSNotificationCenter
不工作。
当用户打开我的应用程序时,将出现标签栏控制器。
- (void)viewWillAppear:(BOOL)animated {
if (false == [[MyClass sharedData] getLoginStatus])
{
LoginViewController *loginViewController = [[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:@"myCustomPopoverLoginVC"];
loginViewController.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentViewController:loginViewController animated:YES completion:^{
}];
...
}
}
- (void)viewDidLoad
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationLogin:) name:@"afterLogin" object:nil];
[super viewDidLoad];
...
}
-(void)notificationLogin:(NSNotification *)notification{
NSLog(@"OhOhOh");
}
在我的登录视图中,
-(IBAction)login:(id)sender{
...
[[NSNotificationCenter defaultCenter] postNotificationName:@"afterLogin" object:nil];
...
}
首先:
- (void)viewWillAppear:(BOOL)animated {
if (false == [[MyClass sharedData] getLoginStatus])
{
LoginViewController *loginViewController = [[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:@"myCustomPopoverLoginVC"];
loginViewController.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentViewController:loginViewController animated:YES completion:^{
}];
...
}
}
- (void)viewDidLoad
{
// Add log here to check when its called
-------> NSLog("Add Observer");
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationLogin:) name:@"afterLogin" object:nil];
[super viewDidLoad];
...
}
-(void)notificationLogin:(NSNotification *)notification{
NSLog(@"OhOhOh");
}
然后在这里添加另一个日志:
-(IBAction)login:(id)sender{
...
-------> NSLog("Post notification");
[[NSNotificationCenter defaultCenter] postNotificationName:@"afterLogin" object:nil];
...
}
所以你可以先看看调用了什么。然后参考@Sandeep Kumar 评论:))
我正在做一个 iPad 项目(xcode 7.21+iOS9)但 NSNotificationCenter
不工作。
当用户打开我的应用程序时,将出现标签栏控制器。
- (void)viewWillAppear:(BOOL)animated {
if (false == [[MyClass sharedData] getLoginStatus])
{
LoginViewController *loginViewController = [[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:@"myCustomPopoverLoginVC"];
loginViewController.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentViewController:loginViewController animated:YES completion:^{
}];
...
}
}
- (void)viewDidLoad
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationLogin:) name:@"afterLogin" object:nil];
[super viewDidLoad];
...
}
-(void)notificationLogin:(NSNotification *)notification{
NSLog(@"OhOhOh");
}
在我的登录视图中,
-(IBAction)login:(id)sender{
...
[[NSNotificationCenter defaultCenter] postNotificationName:@"afterLogin" object:nil];
...
}
首先:
- (void)viewWillAppear:(BOOL)animated {
if (false == [[MyClass sharedData] getLoginStatus])
{
LoginViewController *loginViewController = [[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:@"myCustomPopoverLoginVC"];
loginViewController.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentViewController:loginViewController animated:YES completion:^{
}];
...
}
}
- (void)viewDidLoad
{
// Add log here to check when its called
-------> NSLog("Add Observer");
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationLogin:) name:@"afterLogin" object:nil];
[super viewDidLoad];
...
}
-(void)notificationLogin:(NSNotification *)notification{
NSLog(@"OhOhOh");
}
然后在这里添加另一个日志:
-(IBAction)login:(id)sender{
...
-------> NSLog("Post notification");
[[NSNotificationCenter defaultCenter] postNotificationName:@"afterLogin" object:nil];
...
}
所以你可以先看看调用了什么。然后参考@Sandeep Kumar 评论:))