我必须以编程方式创建一个segue,但是当我点击按钮时,视图控制器不会更改为下一个,故事板ID已设置且仍然无法正常工作。我是否缺少任何其他检查才能使其正常工作?
请参阅以下代码:
EntryViewController *entryController = [self.storyboard instantiateViewControllerWithIdentifier:@"go"];
[self.navigationController pushViewController:entryController animated:YES];
它让我发疯了。感谢
答案 0 :(得分:33)
在我的viewDidLoad中,我放置了调用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];
在goldStarOpen里面,我的代码与你的代码几乎相同。
- (void)goldStarOpen
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:
@"MainStoryboard" bundle:[NSBundle mainBundle]];
UIViewController *myController = [storyboard instantiateViewControllerWithIdentifier:@"GoldStar"];
[self presentViewController:myController animated:YES completion:nil];
}
goldStarOpen在故事板中激活ViewController。
您可能需要设置要加载的View Controller的Storyboard ID。它位于检查器中,位于您为视图控制器指定自定义类的位置下方。
答案 1 :(得分:6)
使用
+ (UIStoryboard *)storyboardWithName:(NSString *)name bundle:(NSBundle *)storyboardBundleOrNil.
所以试试这个
self.storyboard=[UIStoryboard storyboardWithName:@"Your_Story_Board_Name" bundle:[NSBundle mainBundle]];
EntryViewController *entryController = [self.storyboard instantiateViewControllerWithIdentifier:@"go"];
[self.navigationController pushViewController:entryController animated:YES];
答案 2 :(得分:4)
试试这个 故事板选择UIViewController-> Xcode->编辑器 - > Emabed In->导航控制器
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"YourStoryboard"
bundle: nil];
YourViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier: @"YourViewController"];
[self.navigationController pushViewController:vc animated:YES];
答案 3 :(得分:2)
试试这个,
UIStoryboard * storyboardobj=[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
EntryViewController *entryController = [storyboardobj instantiateViewControllerWithIdentifier:@"go"];
[self.navigationController pushViewController:entryController animated:YES];
答案 4 :(得分:0)
您无法设置视图控制器storyboard
。它是只读的,并在创建视图控制器时设置。相反,您需要从其他地方获取故事板或加载它。
您可以从AppDelegate的根视图控制器获取故事板。或者使用+ (UIStoryboard *)storyboardWithName:(NSString *)name bundle:(NSBundle *)storyboardBundleOrNil
。
答案 5 :(得分:0)