Iphone development: how to set a property of another viewController, then get to it

Suppose I have two view controllers, ViewControllerA and ViewControllerB , when I click a button from ViewControllerA , it clicks on ViewControllerB . However, before clicking, I want to set the ViewControllerB property from ViewControllerA . But all I get is nil when I check the variable from ViewControllerB . What am I doing,

In ViewControllerA :

 VCB = [[ViewControllerB alloc]init]; [VCB setPropertyOfViewControllerB:someString]; NSLog(@"value: %@", VCB.PropertyOfViewControllerB); // Here I observe the correct value so I set successfully 

but the fact is, I also want to achieve this from ViewControllerB , but I get nil for the variable.

In ViewControllerB :

 //I already defined in h file as property NSString *PropertyOfViewControllerB; @property(nonatomic, retain) NSString *PropertyOfViewControllerB; 

But when I try to check the value in the viewDidLoad method ViewControllerB

 NSLog(@"value: %@", PropertyOfViewControllerB);//here I get null not the value I set at viewControllerA 

Perhaps I missed a small point that I could not understand. Any help would be awesome. Thanks.


Edit: I am using storyboards. I click the following code:

  UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle: nil]; lvc = [storyboard instantiateViewControllerWithIdentifier:@"mainManu"]; [self.navigationController pushViewController:lvc animated:YES]; 
+4
source share
1 answer

If you use VCB = [[ViewControllerB alloc]init]; but click through the Storyboard, then VCB is not the same ViewController that is used in the Storyboard. Try the following:

  - (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([[segue identifier] isEqualToString:@"yourSegueName"]) { ViewControllerB *vc = [segue destinationViewController]; [vc setPropertyOfViewControllerB:@"foo"]; } } 
+7
source

All Articles