我检查了这里关于堆栈溢出的问题,我用同样的方法做了,但仍然返回 NULL
在第一个 View 中
在 firstviewcontroller.h 我有
@property (nonatomic, copy) NSString *Astring;
在firstviewcontroller.m中
#import "SecondViewController.h"
...
@synthesize Astring = _Astring;
...
- (IBAction)filterSearch:(id)sender {
NSlog(@"%@",Astring)
}
在第二个viewcontroller.m中
#import firstviewcontroller.h
...
...
FirstViewController *controller = [[FirstViewController alloc]initWithNibName:@"FirstViewController" bundle:nil];
controller.Astring = @"YES";
所以基本上我在第一个 View Controller 中创建一个变量并将第二个 View Controller 中的变量传递给第二个 View ,但它总是返回 NULL...
是我的逻辑错了还是别的什么
最佳答案
问题来了。您确实在更改 AString 的字符串值,但是在 View Controller 的新实例上。此行:FirstViewController *controller = [[FirstViewController alloc]initWithNibName:@"FirstViewController"bundle:nil];
正在创建 Controller 的新实例。因此,现有 Controller 的 View Controller 具有相同的属性。
然后您需要做的是找到一种方法来获取您的 firstViewController 的实例。
在 secondViewController.h 中,添加此属性。
#import "firstViewController.h"
...
@property (nonatomic, strong) firstViewController *firstController;
然后,在 secondViewController.m 中,您可以使用 firstController 调用字符串中的更改,它将包含您之后的实例。我相信现在应该是这样的:
firstController.Astring = @"YES"
干杯!
关于ios - 在 xcode 中传递变量返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13705620/