1

I am trying to instantiate a new view controller in my storyboard with this code but it does nothing and stays on the old storyboard.

Here is the code:

(void)menuSelect:(id)sender { UIButton *button = (UIButton*) sender; switch (button.tag) { case 0: { HomeViewController *hc=[[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"Home"]; [self presentViewController:hc animated:YES completion:nil]; break; } 

The storyboard file name is Main.Storyboard and the id for the viewController is Profile.

Here is the image of what the storyboard looks like:

enter image description here

2 Answers 2

5

Issue

You are passing "home" as an indentifier of HomeViewController, while it is "profile". So what you are telling to program is: Get the storyboard with the name "main", instantiate the HomeViewController with identifier "home".

Solution

Change the code to:

HomeViewController *hc=[[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"Profile"]; [self presentViewController:hc animated:YES completion:nil]; 
Sign up to request clarification or add additional context in comments.

Comments

1

Instantiating the storyboard does not cause any View Controller to be displayed. You have to call presentViewController from another view controller.

3 Comments

Ok thanks, do I need to do anything special to remove the previous screen, is it pushing on top or is it just re-creating a new view, because I want to get rid of the old view if necessary.
@Lion789 it isn't replacing the ViewController that presents it, it is added to the stack. You can access the presenting ViewController through the presentingViewController property of the currently visible screen. Likewise you can access the presented ViewController through the previous ViewController through the presentedViewController property.
How would I clear the stack then so a new view is created, because I want to avoid the views staying in memory/having duplicate controllers in the stack.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.