ios - 在 xcode 中传递变量返回 null

标签 ios xcode variables

我检查了这里关于堆栈溢出的问题,我用同样的方法做了,但仍然返回 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/

相关文章:

ios - 为什么cashapelayer的圆路径不是那么圆?

ios - XCTest 在 TestSummaries 中没有 ActivitySummaries

C++变量可以在构造函数中访问,但不能在函数中访问

javascript - 选择多个 jQuery 对象

ios - 正常的 iOS 应用大小?

iphone - 实时调整粒子文件(.plist)

ios - 使控件在 Storyboard 设计器中可见

c - 安装了 XCode,但 GCC 不起作用

c++ - 自动文件重命名 - const 变量正在改变它的值?

iphone - "Admin"苹果账户可以提交应用到苹果商店吗?