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

我正在开发一个应用程序,其主UI基于标签栏控制器。

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

我想要做的是在接收到推送通知时,我想select这个特定的选项卡,从服务器获取最新的数据,find要显示的特定项目,然后将详细视图推到屏幕上以显示所述项目。

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

终止由于未捕获exception“NSGenericException”的应用程序,原因:'无法find一个导航控制器for segue'FavouriteItem'。 只有当源控制器由UINavigationController的一个实例pipe理时,才能使用push segues。

以下是我到目前为止所做的:

应用程序委托应用程序:didReceiveRemoteNotification:

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

从FavouritesViewController – 获取最新的collections夹后,我发送一条消息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放置在一个导航控制器(它embedded在一个故事板里面)当我回应在应用程序委托的推送通知?

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

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

但是我从上面的代码中看不到如何favouritesViewController在tabBarController中呈现。 如果你是在故事板上做的话,只需拖动一个空白的导航控制器,将tabBarController的相关选项卡挂钩到导航控制器(按住Ctrl键拖动,然后select“Relationship segue:viewControllers”,然后从导航控制器到你的FavouritesViewController(同样)。

编辑:

如果这已经在故事板上完成了,那么你需要修改你的代码来拾取现有版本的NHFavouritesViewController ,而不是实例化新的。 就像(假设你有一个引用你的标签栏控制器在self.tabBarController ,而favouritesViewController是在与索引favouritesTab选项卡(我假设你可以得到这些,因为你已经有一个方法来select选项卡):

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

你遇到的问题是你没有实例化导航控制器。

通过使用该方法加载collections夹视图,您实际上只创build一个视图控制器。

所以那么当你告诉它推动它不能因为你没有从故事板实例化导航控制器。

导航控制器已经存在的机会,所以你需要掌握,而不是创build新的控制器。

我现在正在使用移动设备,因此无法完全回答,但是如果您仍在努力,请告诉我,我会看看我是否可以完成代码。 然而,prob将需要首先看到更多的代码。