27

I have to created a segue programmatically however when I click on the button the view controller does not change to the next, Storyboard ID is set and still not working. Am I missing any other checks in order to make this work?

Please see code below:

EntryViewController *entryController = [self.storyboard instantiateViewControllerWithIdentifier:@"go"];
[self.navigationController pushViewController:entryController animated:YES];

its driving me mad. Thanks

2
  • 2
    Is 'self.storyboard' set or is it nil?
    – Wain
    Jun 13, 2013 at 13:19
  • @Wain it is likely set to nil because I havent set it, where do I set the storyboard to self?
    – user2366782
    Jun 13, 2013 at 13:24

6 Answers 6

35

Inside my viewDidLoad I have placed the button which calls goldStarOpen:

UIButton *btnTwo = [UIButton buttonWithType:UIButtonTypeCustom];
btnTwo.frame = CGRectMake(250, 20, 40, 40);
[btnTwo addTarget:self action:@selector(goldStarOpen) forControlEvents:UIControlEventTouchUpInside];
[btnTwo setImage:[UIImage imageNamed:@"GoldStar.png"] forState:UIControlStateNormal];
[self.view addSubview:btnTwo];

Inside goldStarOpen I have code which is almost identical to yours.

- (void)goldStarOpen
{
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:
                                @"MainStoryboard" bundle:[NSBundle mainBundle]];
    UIViewController *myController = [storyboard instantiateViewControllerWithIdentifier:@"GoldStar"];
    [self presentViewController:myController animated:YES completion:nil];
}

goldStarOpen activates a ViewController in the storyboard.

You may need to set the Storyboard ID of the View Controller you are trying to load. This is located in the inspector, just below where you assign a custom class to your view controller.

enter image description here

2
6

Use

 + (UIStoryboard *)storyboardWithName:(NSString *)name bundle:(NSBundle *)storyboardBundleOrNil. 

So try this

self.storyboard=[UIStoryboard storyboardWithName:@"Your_Story_Board_Name" bundle:[NSBundle mainBundle]];
EntryViewController *entryController = [self.storyboard instantiateViewControllerWithIdentifier:@"go"];
[self.navigationController pushViewController:entryController animated:YES];
1
  • I used this answer to instantiate soryboard in AppDelegate. Thanks! Nov 2, 2015 at 0:51
4

try this Storyboard Choose UIViewController-> Xcode->Editor->Emabed In->Navigation Controller

UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"YourStoryboard"
                                                         bundle: nil];

YourViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier: @"YourViewController"];
[self.navigationController pushViewController:vc animated:YES];
2

Try this,

 UIStoryboard *    storyboardobj=[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];

 EntryViewController *entryController = [storyboardobj instantiateViewControllerWithIdentifier:@"go"];
[self.navigationController pushViewController:entryController animated:YES];
1
  • I was looking at another example like this, just tried yours and still no luck!
    – user2366782
    Jun 13, 2013 at 13:46
0

You can't set the view controllers storyboard. It's read only and set when the view controller is created. Instead, you will need to get the storyboard from somewhere else or load it.

You could get the storyboard from the AppDelegate's root view controller. Or use + (UIStoryboard *)storyboardWithName:(NSString *)name bundle:(NSBundle *)storyboardBundleOrNil.

7
  • It is not moving to the next view controller
    – user2366782
    Jun 13, 2013 at 14:13
  • And you checked that the storyboard was created by the call 'storyboardWithName:...'? And the view controller is instantiated for the 'go' identifier? And the current view is in a navigation controller?
    – Wain
    Jun 13, 2013 at 14:39
  • Yes, the storyboard im using is 'mainStoryboard" note the small m! The only thing is that im creating my view programmatically and although my view controller is embedded in a navigation controller when i run it in the emulator, no navigation bar appears at the top, do you think this is a problem?
    – user2366782
    Jun 13, 2013 at 15:21
  • its not crashing so its able to find the storyboard identifier however its not moving
    – user2366782
    Jun 13, 2013 at 15:39
  • can you show the code for how the original controller is created and is the navigation controller nil when you debug?
    – Wain
    Jun 13, 2013 at 18:15
0

Ran into this issue today, clearly set the Storyboard ID on the right panel but I was still getting the warnings that the VC was unreachable and did not have an identifier set. Fixed the issue after I unchecked the box that read "Inherit Module From Target" and ran the project.

enter image description here

After the code successfully ran, it seems to now work with or without the box checked so it might be an Xcode bug... Anyway hope this helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.