4
votes

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

In the first view

in the firstviewcontroller.h i have

@property (nonatomic, copy) NSString *Astring;

in the firstviewcontroller.m

#import "SecondViewController.h"
...
@synthesize Astring = _Astring;
...

- (IBAction)filterSearch:(id)sender {
NSlog(@"%@",Astring)

      }

in the secondviewcontroller.m

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

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

Is my logic wrong or is it something else

1
There's something wrong with your #import, it seems like you inverted them. It won't correct the problem, but the question will be more understandable.rdurand
in NSLog try self.Astring instead of just AstringYarlik
@Yarlik 2bad it is still NULLMichaelAngelo
Besides that is it always needed to make a variable and pass the variable to it...cant you see it in the other view ?MichaelAngelo
Your code is confusing. You declared a property called "AString", then synthesized it assigning "_Astring" as an instance variable. In SecondViewController you set value @"YES" to the property, but in your filterSearch method in NSLog() you use different variable "Astring" which is not a property, or corresponding instance variable. Do you also have a "Astring" variable somewhere or it's just a typo?Yarlik

1 Answers

0
votes

Here's the problem. You are indeed changing the string value of AString, but on a new instance of a view controller. This line: FirstViewController *controller = [[FirstViewController alloc]initWithNibName:@"FirstViewController" bundle:nil]; is creating a new instance of the controller. Therefore, the existing controller's view controller has the same property.

What you need to do then, is find a way to get the instance of your firstViewController.

In secondViewController.h, add this property.

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

Then, in secondViewController.m, you can just call the change in string with firstController, which will contain the instance you're after. I believe it should be something like this now:

firstController.Astring = @"YES"

Cheers!