如何从storyboard中的appdelegate推ViewController
我在我的项目中使用SWRevealViewController ,我想打开一个特定的控制器,当应用程序收到通知。 我已经尝试了很多解决scheme,但没有任何工作
我怎么能显示从我的AppDelegate特定的ViewController?
(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { UIApplicationState appState = UIApplicationStateActive; if ([application respondsToSelector:@selector(applicationState)]) { appState = application.applicationState; } application.applicationIconBadgeNumber = 0; if (appState != UIApplicationStateActive) { SWRevealViewController *navigationController = (SWRevealViewController *)self.window.rootViewController; UINavigationController *nav = (UINavigationController *)navigationController; UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil]; PushNotificationsVC *controller = (PushNotificationsVC*)[mainStoryboard instantiateViewControllerWithIdentifier: @"PushNotificationsVC"]; [nav pushViewController:controller animated:YES]; } else { UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Notification" message:[NSString stringWithFormat:@"%@",[[userInfo objectForKey:@"aps"] valueForKey:@"alert"]] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alertView show]; } }
对于那些通过使用故事板跟随此http://www.appcoda.com/ios-programming-sidebar-navigation-menu/的人。
UIStoryboard *st = [UIStoryboard storyboardWithName:@"Main" bundle: nil]; DestinationController *descController = (DestinationController*)[st instantiateViewControllerWithIdentifier: @"storyboardID_DestController"]; UINavigationController *frontNavigationController = [[UINavigationController alloc] initWithRootViewController:descController]; SidebarViewController *rearViewController = (SidebarViewController*)[st instantiateViewControllerWithIdentifier: @"storyboardID_SidebarMenu"]; RevealViewController *mainRevealController = [[SWRevealViewController alloc] init]; mainRevealController.rearViewController = rearViewController; mainRevealController.frontViewController= frontNavigationController; self.window.rootViewController = mainRevealController;
希望能帮助别人…
那些寻找一个迅速的答案:
var storyboard = UIStoryboard(name: "Main", bundle: nil) var destinationController = storyboard.instantiateViewControllerWithIdentifier("DestinationController") as? DestinationController var frontNavigationController = UINavigationController(rootViewController: destinationController!) var rearViewController = storyboard.instantiateViewControllerWithIdentifier("MenuController") as? MenuController var mainRevealController = SWRevealViewController() mainRevealController.rearViewController = rearViewController mainRevealController.frontViewController = frontNavigationController self.window!.rootViewController = mainRevealController self.window?.makeKeyAndVisible()
SWRevealViewController *navigationController = (SWRevealViewController *)self.window.rootViewController; UINavigationController *nav = (UINavigationController *)navigationController;
修改———
UINavigationController *nav = (UINavigationController *)navigationController.frontViewController; UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil]; PushNotificationsVC *controller = (PushNotificationsVC*)[mainStoryboard instantiateViewControllerWithIdentifier: @"PushNotificationsVC"]; [nav pushViewController:controller animated:YES];
正如用户34336439提到的一些单词缺陷一样,正确的答案是
FrontViewController *frontViewController = [[LiveScoreViewController alloc] init]; RearViewController *rearViewController = [[RearViewController alloc] init]; UINavigationController *frontNavigationController = [[UINavigationController alloc] initWithRootViewController:frontViewController]; UINavigationController *rearNavigationController = [[UINavigationController alloc] initWithRootViewController:rearViewController]; SWRevealViewController *mainRevealController = [[SWRevealViewController alloc] initWithRearViewController:rearNavigationController frontViewController:frontNavigationController]; self.window.rootViewController = mainRevealController;