Passing variables in xcode returns null

I checked the stack overflow questions here and I do it the same way, but still returns NULL

In the first view

in firstviewcontroller.h I have

@property (nonatomic, copy) NSString *Astring; 

in firstviewcontroller.m

 #import "SecondViewController.h" ... @synthesize Astring = _Astring; ... - (IBAction)filterSearch:(id)sender { NSlog(@"%@",Astring) } 

at secondviewcontroller.m

 #import firstviewcontroller.h ... ... FirstViewController *controller = [[FirstViewController alloc]initWithNibName:@"FirstViewController" bundle:nil]; controller.Astring = @"YES"; 

So, I make the variable in the firstviewcontroller and pass the variable to the secondviewcontroller in the second view, but it always returns NULL ...

Am I wrong in my logic or is it something else

+4
source share
1 answer

Here is the problem. You really change the string value of AString, but on a new instance of the view controller. This line: FirstViewController *controller = [[FirstViewController alloc]initWithNibName:@"FirstViewController" bundle:nil]; creates a new controller instance. Therefore, an existing controller controller controller has the same property.

What you need to do is find a way to get an instance of your firstViewController.

In secondViewController.h add this property.

 #import "firstViewController.h" ... @property (nonatomic, strong) firstViewController *firstController; 

Then in secondViewController.m you can simply invoke a row change using firstController, which will indicate the instance that you are using. I believe there should now be something like this:

 firstController.Astring = @"YES" 

Hooray!

0
source

All Articles