-1

I have two storyboards that contain one view controller each.

storyboard1 --------> viewcontrollerA

storyboard2 --------> viewcontrollerB

How do I pass a string from viewcontrollerA to viewcontrollerB?

Example:

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

So far, I know how to transfer views, but I don't know how to transfer data.

This is my code to transfer views:

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

}

My question is, if I have a string in viewcontrollerB called "testString", how do I update that string in the viewcontrollerA IBaction method?

I only know how to transfer data using segues but since this viewcontroller is on another storyboard, a segue - prepareForSegue won't work.

Thank you in advance.

hmzfier
  • 554
  • 9
  • 21
  • possible duplicate of [Passing Data between View Controllers](http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) – Huy Nghia Mar 02 '15 at 05:09
  • declare a strong type of NSString in ViewControllerB. Then access in ViewControllerA as --> `viewcontrollerB.testString = @"Hello ViewController B";` – M Zubair Shamshad Mar 02 '15 at 05:21
  • I've tried all of these solutions but none of them work. This thread uses a lot of depreciated methods and none of them can instantiate a viewcontroller correctly. – hmzfier Mar 02 '15 at 06:35

1 Answers1

1

First of all set Storyboard ID to your SecondView Controller like below image.

enter image description here

Then add following code at your button click event.

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

Storyboard2 is storyboard name.

SecondClassStoryboardID is Storyboard ID which you given in storyboard viewController like above image.

SecondStoryboardVC is the secondclass name which you want to present

Abhishek Sharma
  • 3,283
  • 1
  • 13
  • 22
  • Thank you for your help. Xcode is giving me an error with this line: SecondStoryboardVC * secondStoryboardVCOBJ_ = [storyboard instantiateViewControllerWithIdentifier:@"SecondClassStoryboardID"]; -----> It's saying that "storyboard" is a unknown receiver? – hmzfier Mar 02 '15 at 06:34
  • `SecondStoryboardVC` is the second class name which you want to present. Please see the updated answer. In your example its `viewcontrollerB` – Abhishek Sharma Mar 02 '15 at 07:16
  • @AbhishekSharma What is "setStrValue"? – Supertecnoboff Aug 26 '15 at 11:38