iOS使用故事板推送视图控制器时传递数据

时间:2013-07-21 01:22:30

标签: ios uiviewcontroller uinavigationcontroller storyboard pushviewcontroller

我开始更频繁地使用故事板,但我遇到了问题。通常当您使用UINavigationController推送视图控制器时,您可以通过以下方式执行此操作,并且您可以将数据与其一起传递:

UIViewController *vc = [[UIViewController alloc] init];
vc.link = [self link];
vc.name = [self name];

UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc]
[nav presentViewController:vc completion:nil animated:YES];

(我做了上面的内存代码,所以它可能不是100%正确的。)

现在我正在使用storyboard,我自己并没有实例化视图控制器,所以如何用它传递数据?谢谢!

1 个答案:

答案 0 :(得分:12)

使用prepareForSegue:sender:方法。

例如:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"SegueID"])
    {
        UIViewController * vc = segue.destinationViewController;
        vc.link = [self link];
        vc.name = [self name];
    }
}

或者,如果目标视图控制器是UINavigationController,请使用:

UINavigationController * navigationController = segue.destinationViewController;
UIViewController * vc = [navigationController.viewControllers objectAtIndex:0];