从推送通知启动时,在viewController中启动所有对象

时间:2014-03-24 21:34:16

标签: ios objective-c uinavigationcontroller uistoryboard appdelegate

我有一个viewController X(不是初始视图)。 X是几个父项的childViewController(几个viewController将X作为它们的子项。)在X上,有一个标签,表格和一个带有左栏按钮的导航栏,弹出"弹出"回到它的父母。当应用程序正常启动时,所有segue,popToParentViewController,后退按钮都能正常运行。但是,如果我是通过推送通知启动应用程序,则只显示导航栏和按钮(但不起作用)和表格视图。

我不知道为什么标签没有显示!并且后退按钮不会返回"返回"。我知道这与"没有父母"因为我已将它设置为" root"在我的代码下面

如何使用代码正确实现我想要的内容?

以下是appDelegate.m

- (void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    if ( application.applicationState == UIApplicationStateInactive || application.applicationState == UIApplicationStateBackground  )
{

    UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard"
                                                             bundle: nil];
    NotificationsViewController *viewController= [mainStoryboard instantiateViewControllerWithIdentifier:@"NotificationsViewController"];
    UINavigationController *nav = [[UINavigationController alloc]
                                   initWithRootViewController:viewController];
    [_window setRootViewController:nav];
}
}

2 个答案:

答案 0 :(得分:2)

您需要确保在根目标和目标视图控制器之间创建所有视图控制器并将它们推送到堆栈,只需调用[nav setViewControllers:animated:]或多次调用{{1}在任何一种情况下,您可能都希望确保动画参数为false。

我认为您还需要确保在收到通知时您不会感到不方便,因为所有的旋转操作都会让用户感到非常困惑。

答案 1 :(得分:1)

首先,始终使用- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions方法启动应用程序。您应检查launchOptions字典以检查应用程序是否因通知而被打开。在该启动方法中,您可以通过以下方式访问通知对象:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;
{
    // setup navigation controller here, I assume you already have this in your code

    NSDictionary *dictionary = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];

    if (dictionary != nil)
    {
        NSLog(@"%@: did launch with notification: %@", [self class], dictionary);

        // put the navigation controllers on the nav controller stack as David described
    }
    [[self window] setRootViewController:navigationController];
    [self.window makeKeyAndVisible];

    return YES;
 }

如果应用已经启动,方法- (void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo将被称为 ONLY 。当应用程序从后台切换到前台或运行时,可以调用它。当应用程序作为新进程启动时,永远不会调用此方法。

相关问题