24

我正在创建一个应用程序,其故事板流程如下图所示:

故事板

当用户从“Sysalert 视图控制器”登录时,他们会被带入“消息列表视图控制器”,我在其中执行 NSURLConnection 以将一些 JSON 加载到表中。当用户点击表格中的一行时,他们将进入“消息详细信息”,其中显示该消息的更详细信息。

当用户从推送通知启动应用程序时,无论启动前应用程序的状态如何,我都希望应用程序从我的服务器加载“消息列表”数据,然后向他们显示刚刚推送到设备的消息.

我知道我需要用来didFinishLaunchingWithOptions告诉应用程序对推送通知做出反应,但是如何设置视图层次结构,以便“消息列表”视图控制器加载其数据,然后将“消息详细信息”视图控制器推送到堆栈上适当的信息?

本质上,这种模仿消息或邮件应用程序的行为。打开通知会将您带到该消息的视图控制器,但您仍然可以在层次结构中移回,就好像您已从初始视图控制器启动应用程序并依次遍历视图控制器一样。

4

2 回答 2

8

可以按照您的描述进行操作,但我不建议您这样做。

首先,在情节提要中放置一个带有您想要的视图的断开连接的视图控制器,给视图控制器一个标识符,例如“我的推送通知视图”

didFinishLaunchingWithOptions:中,您可以从应用委托访问 rootViewController。这个控制器将是导航控制器。使用导航控制器,您可以将新的视图控制器推送到堆栈顶部。要创建新的视图控制器,您需要使用标识符“My Push Notification View”来实例化视图控制器。

UINavigationController *navController = (UINavigationController *)self.window.rootViewController;
UIViewController *notificationController = [navController.storyboard instantiateViewControllerWithIdentifier:@"My Push Notification View"];

[navController pushViewController:notificationController animated:YES];

我想你会想要使用类似-presentViewController:animated:completion:显示模式视图的东西,而不是中断导航堆栈。

UIViewController *rootController = (UIViewController *)self.window.rootViewController;
UIViewController *notificationController = [rootController.storyboard instantiateViewControllerWithIdentifier:@"My Push Notification View"];

[rootController presentViewController:notificationController animated:YES completion:NULL];
于 2013-02-13T21:25:35.940 回答
4

试试我在我的一个应用程序中使用的这个,将应用程序委托中的变量作为全局变量

 ex: BOOL gotNotifcation;

-(void)application:(UIApplication*)app didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    NotificationsViewController *notificationobject = [[NotificationsViewController alloc]init];
    [self.navigationController pushViewController:notificationobject animated:YES];
    gotNotifcation = YES;   
}

如果是自定义按钮,则在 NotificationsViewController 中进行后退按钮操作

-(void)gotoback
{
    AppDelegate *delegate =(AppDelegate *)[UIApplication sharedApplication].delegate;

    if(delegate.gotNotifcation)
    {
        delegate.gotNotifcation = NO;
        MessageListController *feed = [[MessageListController alloc] init];
        [self.navigationController pushViewController:feed animated:NO];
    }
    else
    {
        [self.navigationController popViewControllerAnimated:NO];
    }
}
于 2013-12-23T12:00:22.260 回答