Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to push viewcontroller from appdelegate in storyboard

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];
    }
}
like image 288
user3436439 Avatar asked Mar 26 '14 07:03

user3436439


People also ask

How do I push a presented Viewcontroller?

VC3 -> present VC2 -> VC1 VC2 needs to be in a UINavigationController then after you present it you can push VC3. The back button will work as expected on VC3, for VC2 you should call dismiss when the back button is pressed. Try implementing some of that in code and then update your question.

Is AppDelegate a controller?

The application delegate is a controller object. By default, it is the owner and controller of the main window -- which is a view -- in an iOS app. The app delegate receives messages from an object representing -- or modeling -- the application itself (an instance of UIApplication ).

How do I access AppDelegate in Swift?

You could access the AppDelegate through the UIApplication: let delegate = UIApplication. sharedApplication(). delegate as AppDelegate let deviceToken = delegate. deviceToken.


2 Answers

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

like image 143
AdrDev_CTS Avatar answered Oct 02 '22 10:10

AdrDev_CTS


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()
like image 32
user3109460 Avatar answered Oct 02 '22 12:10

user3109460