如何根据远程通知更改ios应用程序中的初始视图控制器?

时间:2013-04-03 00:59:03

标签: iphone cocoa-touch uinavigationcontroller apple-push-notifications

我有一个正在实施远程通知的应用程序。我的愿望是根据以下三个条件之一更改初始视图控制器:用户自己启动应用程序(无通知);或者,用户收到可疑活动警报;或者,用户收到了犯罪警报。我已经在appDelegate中实现了应用程序didReceiveRemoteNotification:方法。根据通知和用户对通知的响应,我实现了以下代码:

UITabBarController *tbContr = (UITabBarController*) self.window.rootViewController;
UINavigationController *navContr = [tbContr.viewControllers][2];
ViewCrimesController *viewCrimes = [navContr.storyboard instantiateViewControllerWithIdentifier:@"ViewCrimes"];
[navContr presentViewController:viewCrimes animated:YES completion:nil];

[self.window.makeKeyAndVisible];

我遇到的问题是导航控件 - 即后退按钮和导航栏标题;它出现时不在ViewCrimesController上。我试图以不同的方式加载ViewCrimesController。每种方式我得到或错误说没有segue(这个视图是映射视图的模型视图)或我试图加载一个活动视图,或者,再次,我没有得到导航控件。

我是否需要专门对导航控件进行编程,或者我在尝试加载视图的方式中遗漏了某些内容?

我见过在其他帖子中动态更改初始视图的引用。但我没有看到任何表明添加控件需要特定编程的东西。非常感谢您提供的任何帮助!

苏珊

2 个答案:

答案 0 :(得分:2)

您是否看过帖子Programmatically set the initial view controller using Storyboards

该帖子的答案可能会解决您的问题,

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];

    UIViewController *viewController = // determine the initial view controller here and instantiate it with [storyboard instantiateViewControllerWithIdentifier:<storyboard id>];

   self.window.rootViewController = viewController;
   [self.window makeKeyAndVisible];

   return YES;
}

您也可以download此示例,并根据您的要求实施。

答案 1 :(得分:0)

只需更改关键窗口的rootViewController,示例代码放在这里,希望它有所帮助:

UIStoryboard *storyboard = self.window.rootViewController.storyboard;
    UIViewController *rootViewController = [storyboard instantiateViewControllerWithIdentifier:@"rootNavigationController"];
    self.window.rootViewController = rootViewController;