ios将通知推送到特定的视图控制器

时间:2015-09-30 13:07:33

标签: ios objective-c uistoryboard

我的应用程序收到远程推送通知,并在收到它们后,我想把它带到一个特定的视图控制器(几乎所有的时间)。我见过这样的代码(在didReceiveRemoteNotification上)。我正在为我的应用程序使用storyboard。

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

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

2 个答案:

答案 0 :(得分:2)

你可以尝试类似的东西:

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

   [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:vc animated:YES completion:nil];

除非你想让uiviewcontroller出现你的pushVC,否则你不需要每次都将uiviewcontroller分配为rootview。

如果您在app delegate中执行此操作,请通过以下方式获取故事板参考:

  UIStoryboard *test=[UIStoryboard storyboardWithName:@"name" bundle:nil];

然后致电

  [test instantiateViewControllerWithIdentifier.....

答案 1 :(得分:0)

您提到的上述代码很好。但是关于如何处理的逻辑依然存在。如果你的推送通知总是需要不同的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];