我根据这些说明将项目从使用XIB迁移到Storyboard:https://stackoverflow.com/a/9708723/2604030 它很顺利。 但我不能让segues以编程方式工作,我需要以这种方式使用它们,因为我有2个按钮链接到同一个ViewController,具有不同的类型,希望你从这个图像中理解为什么。
有2个难度模式按钮。我使用的代码:
`- (IBAction)btnNormalAct:(id)sender {
LevelController *wc = [[LevelController alloc] initWithNibName:@"LevelController" type:0];
[self.navigationController pushViewController:wc animated:YES];
}
- (IBAction)btnTimedAct:(id)sender {
LevelController *wc = [[LevelController alloc] initWithNibName:@"LevelController" type:1];
[self.navigationController pushViewController:wc animated:YES];
}`
当我使用XIB时,这很有用,我确信我在故事板的VC中正确地链接了所有内容。如果我从故事板制作它们,联赛就可以了。但是我该如何处理这种情况。
ALSO:从XIB更改为Storyboard时这些线条是否良好?这是进行此更改的正确方法(上面链接中显示的方式)?
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
}
答案 0 :(得分:5)
您可以使用PrepareForSegue
方法在传入视图控制器调用之前对其进行设置:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Make sure your segue name in storyboard is the same as this line
if ([[segue identifier] isEqualToString:@"YOUR_SEGUE_NAME_HERE"])
{
// Get reference to the destination view controller
LevelController *vc = [segue destinationViewController];
// Pass any objects to the view controller here, like...
[vc setType:1];
}
}
答案 1 :(得分:1)
请勿使用按钮操作。将segue连接到按钮并为segue提供唯一标识符。然后在控制器中实现prepareForSegue:sender:
。当方法触发时,检查seque标识符并在`destinationViewController'上设置适当的类型。
使用故事板时,您应该从故事板中实例化控制器,而不是使用initWithNibName:bundle:
。这是通过为每个视图控制器提供唯一标识符然后在故事板上调用instantiateViewControllerWithIdentifier:
(或者,对于初始视图控制器,只是instantiateInitialViewController
)来完成的,您可以从当前控制器获取(或者如果需要)与storyboardWithName:bundle:
)。