我已成功为我的应用实施推送通知。
我正在尝试完成的是从我的ViewController
向根AppDelegate
发送某种标记,以防应用程序收到PN。
我首先检查了applicationDidBecomeActive:
中的徽章编号,如下所示:
if (application.applicationIconBadgeNumber>0) {
self.hasNotification = YES;
NSLog(@"APNs Message received");
}
现在,我不确定如何将此消息传达给我的根ViewController
,以便触发将用户带到其中一个视图的segue。什么是最好的方法来解决这个问题?
答案 0 :(得分:2)
鉴于许多视图控制器可能对此事件感兴趣,这听起来像是使用NSNotifications提供的发布/订阅模型的一个很好的候选者。
发布通知:
[[NSNotificationCenter defaultCenter] postNotificationName:@"MyEventName" object:optionalPayload];
订阅通知:
- (void)viewWillAppear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:)
name:@"MyEventName" object:nil];
}
- (void)dealloc
{
//Unsubscribe yourself in dealloc
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
-(void)handleNotification:(NSNotification *)pNotification
{
NSLog(@"#1 received message = %@",(NSString*)[pNotification object]);
//Perform your segue here
}
替代方案:自定义根VC
如果您已创建自己的特定于域的容器视图控制器作为根视图控制器,则可以执行以下操作:
我几乎总是在我的应用程序中使用自定义容器 - RootViewController,因为它可以生成可读性很好的代码,可以准确描述正在发生的事情。不仅如此,它还使得从这里实现核心布局(例如滑动菜单等)变得非常简单。