在xcode中传递变量返回null

时间:2012-12-04 14:52:49

标签: ios xcode variables

我在这里检查了堆栈溢出的问题,我以相同的方式执行但仍然返回NULL

在第一个视图中

在firstviewcontroller.h中我有

@property (nonatomic, copy) NSString *Astring;
在firstviewcontroller.m中

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

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

      }
在secondviewcontroller.m

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

所以基本上我在firstviewcontroller中创建一个变量,并将第二个视图传递给第二个视图,但它总是返回NULL ......

我的逻辑错误还是别的什么

1 个答案:

答案 0 :(得分:0)

这是问题所在。您确实在更改AString的字符串值,但是在视图控制器的新实例上。这一行:FirstViewController *controller = [[FirstViewController alloc]initWithNibName:@"FirstViewController" bundle:nil];正在创建控制器的新实例。因此,现有控制器的视图控制器具有相同的属性。

您需要做的是找到获取firstViewController实例的方法。

在secondViewController.h中,添加此属性。

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

然后,在secondViewController.m中,您可以使用firstController调用string中的更改,firstController将包含您之后的实例。我相信它现在应该是这样的:

firstController.Astring = @"YES"

干杯!