ios向特定视图控制器推送通知

ios向特定视图控制器推送通知,ios,objective-c,uistoryboard,Ios,Objective C,Uistoryboard,我的应用程序接收远程推送通知,收到通知后,我想将其发送到特定的视图控制器(几乎一直如此)。我见过这样的代码(关于DidReceiveMotonification)。我正在为我的应用程序使用故事板 UIViewController *vc = self.window.rootViewController; PushViewController *pvc = [vc.storyboard instantiateViewControllerWithIdentifier:@"someid

我的应用程序接收远程推送通知,收到通知后,我想将其发送到特定的视图控制器(几乎一直如此)。我见过这样的代码(关于DidReceiveMotonification)。我正在为我的应用程序使用故事板

    UIViewController *vc = self.window.rootViewController;
    PushViewController *pvc = [vc.storyboard instantiateViewControllerWithIdentifier:@"someid"];
    [vc presentViewController:pvc animated:YES completion:nil];

这似乎很简单。但它是否会对内存分配产生任何影响,因为每次推送通知到达时,我们都会实例化一个新的视图控制器。这是最好的做法吗

您可以尝试以下方法:

    PushViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"someid"];

   [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:vc animated:YES completion:nil];
您不需要每次都将uiviewcontroller分配为rootview,除非您希望uiviewcontroller呈现您的pushVC

如果您正在执行此应用程序内委托,则可以通过以下方式获取情节提要引用:

  UIStoryboard *test=[UIStoryboard storyboardWithName:@"name" bundle:nil];
然后打电话

  [test instantiateViewControllerWithIdentifier.....

上面提到的代码很好。但如何处理这一问题仍取决于你自己。如果您总是希望推送通知使用不同的VC,那么这很好

比如说,如果你立即收到两个推送通知,两个VC将立即出现。但是你只需要一个屏幕就可以进行推送,那么这就有麻烦了

如果希望有一个pushViewController对象,则可以将其设置为全局对象并执行如下简单检查

UIViewController *vc = self.window.rootViewController;
if(!pvc)
{
   pvc = [vc.storyboard instantiateViewControllerWithIdentifier:@"someid"];
}
[vc updatePushViewControllerDependingOnPushNotificationObject:somePushObject];
[vc presentViewController:pvc animated:YES completion:nil];

你为什么要将rootview分配给vc?你不能使用self.storyboard吗?这只是我在网上看到的一个例子…你有没有一个例子?T先生,每次推送发生时,我们仍在实例化新的视图控制器?好的,谢谢。我要试试这个。