How to push viewcontroller from appdelegate in sto

2019-03-15 08:20发布

问题:

I am using SWRevealViewController in my project, and I want to open a particular controller when the app receives a notification. I have tried so many solutions but nothing works.

How can I show a specific ViewController from my 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:

For those who following this http://www.appcoda.com/ios-programming-sidebar-navigation-menu/ by using storyboard.

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;

Hope it help someone else...



回答2:

Those looking for a swift answer:

    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;

modify ---------

UINavigationController *nav = (UINavigationController *)navigationController.frontViewController;



UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
PushNotificationsVC *controller = (PushNotificationsVC*)[mainStoryboard instantiateViewControllerWithIdentifier: @"PushNotificationsVC"];
[nav pushViewController:controller animated:YES];


回答4:

As the user34336439 mentioned with some word defects, the correct answer is

    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;