instantiateViewControllerWithIdentifier - Storyboard id set but still not working

十年热恋 提交于 2019-12-17 19:07:08

问题


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


回答1:


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.




回答2:


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];



回答3:


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];



回答4:


Try this,

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

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



回答5:


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.




回答6:


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.

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.



来源:https://stackoverflow.com/questions/17088057/instantiateviewcontrollerwithidentifier-storyboard-id-set-but-still-not-workin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!