2

I migrated a project from using XIB's to Storyboard, according to these instructions: https://stackoverflow.com/a/9708723/2604030 It went good. But I can't make the segues work programmatically, and I need to use them this way, because I have 2 buttons that link to the same ViewController, with different types, hope you understand why from this image.

enter image description here There are 2 difficulty mode buttons. The code I use:

`- (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];
}`

This worked when I used XIB's, and I am sure I linked everything correctly in the storyboard's VCs. The seagues works if I make them from the storyboard. But how can I manage this situation.

ALSO: are those lines good when changing from XIB's to Storyboard? Is that the right way to do this change (the way shown in the link above)?

    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
}
4

2 回答 2

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];
    }
}
于 2013-08-22T18:51:27.720 回答
1

不要使用按钮操作。将 segues 连接到按钮并为 segues 提供唯一标识符。然后prepareForSegue:sender:在你的控制器中实现。当方法触发时,检查序列标识符并在“destinationViewController”上设置适当的类型。

使用情节提要时,您应该从情节提要中实例化您的控制器,而不是使用initWithNibName:bundle:. 这是通过给每个视图控制器一个唯一的标识符,然后在情节提要上调用instantiateViewControllerWithIdentifier:(或者,对于初始视图控制器,只是instantiateInitialViewController)来完成,您可以从当前控制器(或者如果需要的话storyboardWithName:bundle:)获得。

于 2013-08-22T18:52:16.407 回答