Get ViewController from Storyboard
Solution 1
You should pass your data successively through your UIViewController
s navigation. If, for example, you have a navigation like FirstVC > SecondVC > ThirdVC
:
In your FirstVC.m, use :
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
((SecondVCClass*) segue.destinationViewController).secondVCString = _firstVCString;
}
With secondVCString
being a @property
in your second ViewController.
In your SecondVC.m, use :
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
((ThirdVCClass*) segue.destinationViewController).thirdVCString = _secondVCString;
}
With of course thirdVCString
being a @property
in your third ViewController.
Edit:
As you updated your question, here is what I suggest :
In your MenuVC, add this :
@property (nonatomic, weak) NSString *importantString;
In your FirstVC and SecondVC, add this :
@property (nonatomic, weak) MenuVCClass *menu;
When you push to FirstVC or SecondVC, use
prepareForSegue
to set the destination's view controllermenu
property to your menu :-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([segue.identifier isEqualToString:@"FirstSegue"]) ((FirstVC*) segue.destinationViewController).menu = self; else if ([segue.identifier isEqualToString:@"SecondSegue"]) ((SecondVC*) segue.destinationViewController).menu = self; }
In FirstVC or SecondVC, you can change the
NSString
value from your menu using_menu.importantString = @"";
Solution 2
You should never use new
to create a view controller.
If you're using storyboards but not using segues, you can still create a view controller from the storyboard and invoke it.
Use the method instantiateViewControllerWithIdentifier:
to create an instance of the target view controller. The set the properties you want to set, and finally make a call to display it (present it modally, push it onto the navigation stack, or whatever is appropriate for your program.)
Sam
Updated on July 09, 2022Comments
-
Sam almost 2 years
I have two ViewControllers, which aren`t (directly) connected with a Segue. But I want to change a String in the second VC, which i get in the first one. My try was:
#import "secondViewController.h" @interface firstViewController () @property NSString* originalString; @end @implementation firstViewController -(void)viewDidDisappear:(BOOL)animated{ [super viewDidDisappear:animated]; secondViewController* svc = [secondViewController new]; svc.anotherString = self.originalString; }
But it dosent work, because I've only created a instance of the second VC, so the value was not saved. Also I can`t use the Storyboard ID, because I use Xcode 5.
I have a menuVC from which you can get to the firstVC and the secondVC. And from the firstVC I can go back (with the navigationbackbarbutton) to the menu. so: menu->firstVC->menu. menu->secondVC->...->menu
My try with StoryboardID:
-(void)viewDidDisappear:(BOOL)animated{ [super viewDidDisappear:animated]; secondViewController* svc =[[UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil] instantiateViewControllerWithIdentifier:@"secondVCSrorybradID"]; svc.anotherString = self.originalString; }
-
Sam about 10 yearsthanks, but my first and my sec on VC goes both from another VC so when I get back to my menu the method "prepareForSegue" won't be called.
-
rdurand about 10 yearsCan you edit your question with your app's structure ? Something like FirstVC -- SecondVC -- etc.
-
Duncan C about 10 yearsCan't use a storyboard ID you mean? Why? You said "because I use Xcode 5", which does not make any sense.
-
Sam about 10 yearsbut i haven't any storryboard ID, really... I've only Title
-
Duncan C about 10 yearsOk, but you can (and should) assign a storyboard ID to any scene in your s storyboard. This is supported from Xcode 4.0 to the current version, 5.0.2
-
Duncan C about 10 yearsDude, you need to write sentences that other people can understand if you actually want help. "It wouldn't be saved" is not one of those sentences.
-
Sam about 10 yearsYES it works!! thanks :D Not exactly like you but it was your main idea with the menuVC thanks soooooooooo much!! §> U