Question

I'm developing an app for iPhone. My problem is, I have a uiviewcontroller that has something like a shortcuts menu. From there, I want to be able to switch page to any on storyboard but just presenting it like this

[self.mainID dismissViewControllerAnimated:YES
                         completion:^{
                             checkin_vc *sampleView = [[checkin_vc alloc] init];
                             [self.mainID presentViewController:sampleView animated:YES completion:nil];
                         }];

doesn't seem to cut it. nothing happens, and if I try to present it without dismissing the current controller first, then I get a black screen, most likely because I'm trying to present an object of class, not the actual view controller that is set up on the storyboard.

i tried

[self.mainID dismissViewControllerAnimated:YES
                         completion:^{
                             UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
                             checkin_vc *sampleVIew = (checkin_vc *)[mainStoryboard instantiateViewControllerWithIdentifier:@"checkin_sid"];
                             [self.mainID presentViewController:sampleVIew animated:YES completion:nil];
                         }];

dismiss is done and I'm returned to previous view controller, but completion part never gets done.

I also get this warning:

2014-05-12 00:05:31.388 Roborder[5503:60b] Warning: Attempt to present on whose view is not in the window hierarchy!

Was it helpful?

Solution

My experience with storyboards is that instantiating via alloc init isn't going to work. Firstly make sure that every view controller you may call has, in the storyboard, a storyboardID set. It's just a unique string but you can use it as below to instatiate:

UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
checkin_vc *sampleVIew = (checkin_vc *)[mainStoryboard instantiateViewControllerWithIdentifier:@"checkin_vc"];
//present as original code.

It's late though so let me know if I'm misunderstanding the question :)

OTHER TIPS

  1. Cocoadelica's code is correct, but you need give your checkin_vc an Identifier in the storyboard to present it-- check the left panel -- show the identity inspector -- put a string in "Storyboard ID" (for example: checkin view controller)-- then you can use it in the code like this

    checkin_vc *sampleVIew = (checkin_vc *)[mainStoryboard instantiateViewControllerWithIdentifier:@"checkin view controller"];

  2. you should not dismiss the shortcut menu(if it's your root menu) cause you're checkin_vc will lost it's parent(viewcontroller).

  3. here's my sample code of viewcontroller switch, hope it will help you:

in the MenuViewController.m

    -(IBAction)GotoShop:(id)sender {
      ShopViewController *viewController = [self.storyboard instantiateViewControllerWithIdentifier:@"ShopController"];
      [self presentViewController:viewController animated:YES completion:nil];
     }

this will present the shopViewController

and in the ShopViewController.m

-(IBAction)QuitShop:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

This will dismiss the shopViewController and return to it's parentviewcontroller --- in this case back to MenuViewController

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top