Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 当收到推送通知后应用程序变为活动状态时,在主ViewController上触发事件_Ios_Objective C_Apple Push Notifications - Fatal编程技术网

Ios 当收到推送通知后应用程序变为活动状态时,在主ViewController上触发事件

Ios 当收到推送通知后应用程序变为活动状态时,在主ViewController上触发事件,ios,objective-c,apple-push-notifications,Ios,Objective C,Apple Push Notifications,我已成功为我的应用程序实施推送通知。 我现在试图完成的是从我的AppDelegate向根ViewController发送某种标志,以防应用程序收到PN 我首先在我的应用程序IDBecomeActive:中检查徽章编号,如下所示: if (application.applicationIconBadgeNumber>0) { self.hasNotification = YES; NSLog(@"APNs Message received");

我已成功为我的应用程序实施推送通知。 我现在试图完成的是从我的
AppDelegate
向根
ViewController
发送某种标志,以防应用程序收到PN

我首先在我的
应用程序IDBecomeActive:
中检查徽章编号,如下所示:

if (application.applicationIconBadgeNumber>0) {
        self.hasNotification = YES;
        NSLog(@"APNs Message received");            
    }

现在,我不知道如何将此消息传递给我的root
ViewController
,以便触发一个将用户带到其中一个视图的序列。最好的方法是什么

考虑到许多视图控制器可能对该事件感兴趣,这似乎是使用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
}
订阅通知:

[[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,因为它可以生成可读性很好的代码,准确地描述正在发生的事情。不仅如此,它还使得从这里实现核心布局(如滑动菜单等)变得非常简单