在接收推送通知时打开特定视图

时间:2011-06-01 07:51:11

标签: iphone push-notification navigationcontroller

我有UITableView作为我的rootViewController,我用解析后的RSS填充该表(有一个Parser类,其中rootViewController是其委托。在rootViewController我有刷新RSS refreshData的方法,并将我的检索数据保存在静态MutableArray staticItems中:

单击tableView单元格中的单元格时,detailView同时推送navigationController(同时选择单元格(行))我创建并传递字典{ {1}}到theItem。在该字典中,我传递了detailViewstaticItems(所选单元格的索引)中的值。通过这种方式,我可以显示新闻的文本,并在新闻阵列中跟踪新闻的位置,以实现幻灯片上一页/下一页。

现在,我启用了推送通知,在收到推送通知时,我的应用程序又返回到前台,但上次应用关闭时打开了该视图。

我想通过重新解析(刷新)RSS并显示最后的新闻(theItem [0])来在 detailView 中呈现最新消息。

所以,我想得到以下结果:调用positionInArray然后选择单元格中的第一个项目并在[rootController refreshData]中打开它

我一直在使用委托方法detailView,但我找不到让它工作的方法。我尝试创建新的didReceiveRemoteNotification,然后将其堆叠在现有的rootController上:(。

请与我分享您的想法:)

1 个答案:

答案 0 :(得分:3)

首先,这个问题根本与推送通知无关。这是一个如何从应用程序委托中的任意位置访问视图控制器的问题。

您最好的(也可能是唯一的)赌注是手动保留对相关视图控制器实例的引用。

我假设您使用UINavigationController,其中根是您的列表,然后您将详细视图控制器推送到它上面。在您的应用代理中保留对此导航控制器的引用。将@property (nonatomic, retain) UINavigationController *mainNavController;添加到您的应用程序代理中。创建导航控制器时,请指定它,以便应用程序委托具有参考。

MyAppDelegate *ad = ((MyAppDelegate *)[UIApplication sharedApplication].delegate);
ad.mainNavController = theNavController;

如果您在app delegate中创建导航控制器,您显然只需要这样做:

self.mainNavController = theNavController;

然后,当您收到推送通知时,只需直接对导航控制器执行操作。

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    // Do whatever you need to do in order to create an instance of your
    // detail view controller
    MyDetailViewController *vc = [MyDetailViewController magicalStuff:userInfo];

    // Add the detail view controller to the stack, but keep the root view
    // controller.
    UIViewController *root = self.mainNavController.topViewController;
    NSArray *vcs = [NSArray arrayWithObjects:root, vc, nil];
    [self.mainNavController setViewControllers:vcs animated:YES];
}

然后导航控制器将通过滑动设置为MyDetailViewController的动画,后退按钮将带您进入列表。