如何从故事板中的appdelegate推送viewcontroller

时间:2014-03-26 07:45:08

标签: ios iphone swrevealviewcontroller

我在我的项目中使用SWRevealViewController,我想在应用收到通知时打开一个特定的控制器。我尝试了很多解决方案,但没有任何效果。

如何从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];
    }
}

4 个答案:

答案 0 :(得分:13)

对于那些使用故事板来关注此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;

希望它可以帮助别人...

答案 1 :(得分:5)

那些寻求快速回答的人:

    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()

答案 2 :(得分:1)

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];

答案 3 :(得分:0)

正如用户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;