ViewController如果从App Delegate启动,则显示没有导航栏

时间:2014-03-21 01:10:03

标签: ios model-view-controller uinavigationcontroller apple-push-notifications appdelegate

因此,当用户按下推送通知时,我尝试加载NotificationsViewController。在我的应用程序中,我尝试加载的viewController有一个导航栏(包含一个弹回到上一个视图的后退按钮)。我在appDelegate.m

中实现了以下代码
- (void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
if ( application.applicationState == UIApplicationStateInactive || application.applicationState == UIApplicationStateBackground  )
{
    UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard"
                                                             bundle: nil];
    UINavigationController *nav = [mainStoryboard instantiateViewControllerWithIdentifier:@"NotificationsViewController"];
    [_window setRootViewController:nav];
}
}

现在当我按下推送通知时,它会启动到右视图控制器。但是,它没有我的导航栏。我该如何解决这个问题?感谢

1 个答案:

答案 0 :(得分:1)

由于NotificationsViewController嵌入在导航控制器中,您应该将其实例化并将其设置为窗口的根视图控制器,

- (void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    if ( application.applicationState == UIApplicationStateInactive || application.applicationState == UIApplicationStateBackground  ) {
        UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle: nil];
        UINavigationController *nav = [mainStoryboard instantiateViewControllerWithIdentifier:@"Nav"];
       [_window setRootViewController:nav];
    }
}

请务必为导航控制器提供与您在此处使用的标识符匹配的标识符。当导航控制器成为窗口的根视图控制器时,它将显示其topViewController,这可能是你的NotificationsViewController。