12

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];
    }
}
JW Lim
  • 1,794
  • 3
  • 21
  • 41
user3436439
  • 121
  • 1
  • 5
  • You should read the [FAQ](http://stackoverflow.com/help/on-topic) to ask good questions. – rdurand Mar 26 '14 at 07:50
  • I have a slide menu in my project for that i have use SWRevealViewController component. And when i get the push notification i want to open a particular screen(ViewController). I am no able to open a particular controller from the appdelegate .(applicationDidReceiveNotification).Are you geting what i am try to saying?? – user3436439 Mar 26 '14 at 07:52
  • If you have any idea then plz do reply – user3436439 Mar 26 '14 at 07:57
  • did you try this : `[self.window.rootViewController presentViewController:MyCustomViewController animated:NO completion:nil]` ? Can you show us what do you have in `-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo;` ? – Ali Abbas Mar 26 '14 at 08:17

4 Answers4

13

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...

AdrDev_CTS
  • 245
  • 4
  • 11
5

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()
user3109460
  • 181
  • 2
  • 2
1
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];
Pranit Kothari
  • 9,721
  • 10
  • 61
  • 137
  • 1
    NavigationViewController *frontNavigationController = [NavigationViewController alloc] initWithRootViewController:frontVC]; NavigationViewController *rearNavigationController = [[NavigationViewController alloc] initWithRootViewController:rearVC]; SWRevealViewController *mainRevealController = [[SWRevealViewController alloc] initWithRearViewController:rearNavigationController frontViewController:frontNavigationController]; I have found the solution Thanks everybody for the giving efforts. I am not able to give vote up to anyone sorry. – user3436439 Jun 14 '14 at 05:35
0

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;
lomec
  • 1,356
  • 1
  • 11
  • 10