Xcode将数据从viewcontroller传递到另一个storyboard中的另一个viewcontroller

时间:2015-03-02 05:05:22

标签: ios objective-c xcode uiviewcontroller uistoryboardsegue

我有两个故事板,每个故事板包含一个视图控制器。

storyboard1 --------> viewcontrollerA

storyboard2 --------> viewcontrollerB

如何将一个字符串从viewcontrollerA传递给viewcontrollerB?

示例:

viewcontrollerB.testString=@"The transfer of data worked!";

到目前为止,我知道如何传输视图,但我不知道如何传输数据。

这是我传输视图的代码:

- (IBAction)moveToViewcontrollerB:(id)sender {

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

UIViewController *viewcontrollerB = [storyboard2 instantiateInitialViewController];

viewcontrollerB.modalTransitionStyle = UIModalTransitionStyleCoverVertical;

[self presentViewController:lessonOne_ViewController animated:YES completion:nil];

}

我的问题是,如果我在viewcontrollerB中有一个名为“testString”的字符串,我如何在viewcontrollerA IBaction方法中更新该字符串?

我只知道如何使用segue传输数据,但由于此viewcontroller位于另一个故事板上,因此segue - prepareForSegue将无效。

提前谢谢。

1 个答案:

答案 0 :(得分:1)

首先将Storyboard ID设置为SecondView Controller,如下图所示。

enter image description here

然后在code活动中添加以下button click

- (IBAction)moveToViewcontrollerB:(id)sender {
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Storyboard2"
                                                         bundle: nil];
    SecondStoryboardVC * secondStoryboardVCOBJ_ = [storyboard instantiateViewControllerWithIdentifier:@"SecondClassStoryboardID"];
    [secondStoryboardVCOBJ_ setStrValue:@"The transfer of data worked!"];
    secondStoryboardVCOBJ_.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    [self presentViewController:secondStoryboardVCOBJ_ animated:YES completion:nil];
}

Storyboard2storyboard名称。

SecondClassStoryboardID Storyboard ID是您在storyboard viewController中提供的,如上图所示。

SecondStoryboardVC是您要secondclass

present名称