iOS - 收到推送通知后显示视图

问题描述:

我正在开发一个主要 UI 基于标签栏控制器的应用.

I am working on an app whose main UI is based on a tab bar controller.

在其中一个选项卡中,我有一个集合视图,它通过导航控制器深入到详细视图.

In one of the tabs I have a collection view, which drills down to a detail view via a navigation controller.

我想要做的是在收到推送通知后,我想选择此特定选项卡,从服务器获取最新数据,找到要显示的特定项目,然后将详细信息视图推送到屏幕上显示所述项目.

What I am trying to do is upon receipt of a push notification I would like to select this specific tab, fetch the latest data from the server, find the particular item to display, then push the detail view on to the screen to display said item.

我的问题是我在 collectionView:didSelectItemAtIndexPath 后收到以下消息:

My problem is I get the following message after collectionView:didSelectItemAtIndexPath:

由于未捕获的异常NSGenericException"而终止应用程序,原因:'找不到 segue 的导航控制器'收藏夹'.Push segues 只能在 source 时使用控制器由 UINavigationController 的一个实例管理.'

Terminating app due to uncaught exception 'NSGenericException', reason: 'Could not find a navigation controller for segue 'FavouriteItem'. Push segues can only be used when the source controller is managed by an instance of UINavigationController.'

这是我目前所做的:

应用委托应用:didReceiveRemoteNotification:

App Delegate application:didReceiveRemoteNotification:

[self selectFavouritesTab];
NHFavouritesViewController *favouritesViewController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"Favourites"];
[favouritesViewController displayFavouriteForPushNotificationWithId:favouriteId];

从 FavouritesViewController - 获取最新的收藏夹后,我向 displayFavouriteItemWithId 发送消息:

From FavouritesViewController - After fetching the latest favourites, I send a message to displayFavouriteItemWithId:

- (void)displayFavouriteItemWithFavouriteId:(NSNumber*)favouriteId
{
    NSArray* results = [_collectionViewData filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF.favouriteId == %@", favouriteId]];

    NSInteger row = [_collectionViewData indexOfObject:[results lastObject]];

    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:0];
    [[self collectionView] selectItemAtIndexPath:indexPath animated:YES scrollPosition:UICollectionViewScrollPositionNone];
    [self.collectionView.delegate collectionView:self.collectionView didSelectItemAtIndexPath:indexPath];
    [self performSegueWithIdentifier:@"FavouriteItem" sender:self];
}

就在此时它崩溃了.我明白崩溃消息在说什么,但是我不知道如何在我响应应用程序委托中的推送通知时将 NHFavouritesViewController 放置在导航控制器(嵌入在故事板中)中?

And it is at this point it crashes. I understand what the crash message is saying, however what I don't know is how to place NHFavouritesViewController inside a navigation controller (which is embedded inside one in the storyboard) when I respond to the push notification in the app delegate?

您可以将视图控制器包装在标准导航控制器中:

You can wrap a view controller in a standard navigation controller with:

UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:favouritesViewController];

但是我无法从上面的代码中看到 favouritesViewController 在 tabBarController 中是如何呈现的.如果您在故事板中进行操作,则只需拖入一个空白的导航控制器,将 tabBarController 的相关选项卡挂钩到导航控制器(按住 Ctrl 键拖动,然后选择关系 segue:viewControllers",然后从导航控制器挂钩到您的 FavoritesViewController(同样).

But I can't see from your code above how favouritesViewController is presented in the tabBarController. If you are doing it in a storyboard, then just drag in a blank navigation controller, hook the relevant tab of your tabBarController to the navigation controller (Ctrl-drag, then select "Relationship segue: viewControllers", and then hook from the navigation controller to your FavouritesViewController (likewise).

如果这已经在故事板中完成了,那么您需要修改您的代码以获取NHFavouritesViewController 的现有版本,而不是实例化新的.类似于(假设您在 self.tabBarController 中有对 Tab Bar Controller 的引用,并且 favouritesViewController 位于带有索引 favouritesTab 的标签中(我假设您可以获得这些,因为您已经有了选择选项卡的方法):

If that is already done in the storyboard, then you need to amend your code to pickup the existing version of NHFavouritesViewController, instead of instantiating new. Something like (assuming you have a reference to your Tab Bar Controller in self.tabBarController, and the favouritesViewController is in the tab with index favouritesTab (I assume you can get these, since you already have a method to select the tab):

UINavigationController *navController = (UINavigationController *)self.tabBarController.viewControllers[favouritesTab];
NHFavouritesViewController *favouritesViewController = (NHFavouritesViewController *) navController.rootViewController;