Как нажимать viewcontroller из appdelegate в раскадровке

Я использую SWRevealViewController в своем проекте, и я хочу открыть конкретный контроллер, когда приложение получает уведомление. Я пробовал так много решений, но ничего не работает.

Как я могу показать определенный ViewController из моего AppDelegate?

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

Ответ 1

Для тех, кто следит за 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;

Надеюсь, что это поможет кому-то еще...

Ответ 2

Те, кто ищет быстрый ответ:

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

Ответ 3

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

Ответ 4

Как указал пользователь 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;