Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to push viewcontroller from appdelegate in storyboard inside navigation controller

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.

I follow this http://www.appcoda.com/ios-programming-sidebar-navigation-menu/ by using storyboard. My storyboard is designed as below:

Storyboard

When the application receives a notification I want to load Photo view controller within its navigation controller. I tried with the following code in the AppDelegate:

UIStoryboard *st = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
    photoViewController *descController = (PhotoViewController*)[st instantiateViewControllerWithIdentifier: @"photoView"];
    UINavigationController *frontNavigationController = [[UINavigationController alloc] initWithRootViewController:descController];
    SidebarTableViewController *rearViewController = (SidebarTableViewController*)[st instantiateViewControllerWithIdentifier: @"menuController"];

    SWRevealViewController *mainRevealController = [[SWRevealViewController alloc]  init];

    mainRevealController.rearViewController = rearViewController;
    mainRevealController.frontViewController= frontNavigationController;
    self.window.rootViewController =nil;
    self.window.rootViewController = mainRevealController;
    [self.window makeKeyAndVisible];

This works, but creates a new Navigtion controller, and what I need is to use the one already defined in the storyboard, since it has specific properties.

Any idea?

Thanks

like image 528
Kepa Santos Avatar asked Sep 01 '15 12:09

Kepa Santos


People also ask

How do I set the identifier for ViewController in storyboard?

Step 1: Set a Storyboard ID In the Storyboard, select the view controller that you want to instantiate in code. Make sure the yellow circle is highlighted, and click on the Identity Inspector. Set the custom class as well as the field called "Storyboard ID". You can use the class name as the Storyboard ID.

How do I embed a view controller in navigation controller storyboard?

In your storyboard, select the initial view controller in your hierarchy. With this view controller selected, choose the menu item Editor -> Embed In -> Navigation Controller .

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.


Video Answer


2 Answers

actually SwLRevalViewController Taken the ownership of Root, so do like this

in Appdelegate.m

//initially navigate to your Main controller on SWL

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler
{

   NSString *alert = userInfo[@"aps"][@"redirect_url"];
[[NSUserDefaults standardUserDefaults]setObject:alert forKey:@"itemType"];
 [[NSUserDefaults standardUserDefaults]synchronize];
 UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
SWRevealViewController *main = (SWRevealViewController *)[mainStoryboard instantiateViewControllerWithIdentifier:@"SWRevealViewController"];

    self.window.rootViewController = main;
 }

//it automatically redirect to your root controller

on that main view controller ViewDidLoad check / Navigate to your photo view controller

        NSString *getType=[[NSUserDefaults standardUserDefaults]objectForKey:@"itemType"];
 if ([gettypeofItem isEqualToString:@"something"])
        {
   yourPhotoViewController *ivc=[self.storyboard instantiateViewControllerWithIdentifier:@"yourPhotoViewController"];

   [self.navigationController pushViewController:ivc animated:YES];
    }
  else

  {

   // do nothing 

  }
like image 89
Anbu.Karthik Avatar answered Oct 01 '22 04:10

Anbu.Karthik


You can use following method

NSString *storyBoardName=@"Main_iPad";
if (isPhone)
    storyBoardName=@"Main_iPhone";
UIStoryboard *storyBoard=[UIStoryboard storyboardWithName:storyBoardName bundle:nil];

PlayVideoBySocketsScene *controller=[storyBoard instantiateViewControllerWithIdentifier:@"playVideoUsingSockets"];


float disptachTime=2.0;
if (value)
{
    disptachTime=1.0;
}
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(disptachTime * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    [[[[UIApplication sharedApplication] keyWindow] rootViewController]  presentViewController:controller animated:YES completion:nil];
like image 35
Arslan Asim Avatar answered Oct 01 '22 04:10

Arslan Asim