简体   繁体   English

当我尝试从另一个视图控制器(Objective-c?)访问变量时,为什么变量为null?

[英]Why is my variable null when I am attempting to access it from another view controller (Objective-c?

I am trying to use the .text of UITextField in my first view controller in another .text of UITextField in my second view controller, but my first view controller firstTField.text turns out to be (null) in my second view controller even though I printed _firstTField.text in my first view controller and it printed out the input that was entered. 我正在尝试在第二个视图控制器的另一个UITextField的.text中使用我的第一个视图控制器的UITextField的.text,但是我的第一个视图控制器firstTField.text在我的第二个视图控制器中却变成了(null),即使我在我的第一个视图控制器中打印了_firstTField.text,并打印出了输入的内容。

What may be the problem? 可能是什么问题? Why is null? 为什么为空? Could someone guide me on how to approach/resolve this? 有人可以指导我如何解决/解决这个问题吗?

FirstViewController.h FirstViewController.h

@property (weak, nonatomic) IBOutlet UITextField *firstTField;

SecondViewController.h SecondViewController.h

@property (weak, nonatomic) IBOutlet UITextField *secondTField;

SecondViewController.m SecondViewController.m

#import "FirstViewController.h"

- (void)viewDidLoad {
     [super viewDidLoad];
     FirstViewController *firstPage = [[FirstViewController alloc] init];
     _secondTField.text = firstPage.firstTField.text;
}

I am going to assume that FirstViewController segues to SecondViewController . 我会认为FirstViewController塞格斯到SecondViewController There are two things going on with your code that will prevent secondTField from having its text set: 您的代码发生了两件事,这将阻止secondTField设置其文本:

1) You are instantiating a new instance of FirstViewController instead of referencing the one that you segued from. 1)您正在实例化FirstViewController实例,而不是引用您从中FirstViewController实例。 This new FirstViewController will not be in the view hierarchy and therefore will not have its text field set. 此新的FirstViewController将不在视图层次结构中,因此不会设置其文本字段。

2) You will need to use an NSString during the segue to hold that information temporarily in SecondViewController while it gets loaded into the view hierarchy. 2)您将需要在搜索过程中使用NSString在将信息加载到视图层次结构时将其暂时保存在SecondViewController

Your code would then look like this: 您的代码将如下所示:

FirstViewController.h FirstViewController.h

@property (weak, nonatomic) IBOutlet UITextField *firstTField;

SecondViewController.h SecondViewController.h

@property (weak, nonatomic) IBOutlet UITextField *secondTField;
@property (strong, nonatomic) NSString *secondTFieldText;  // temporary to hold the text from FirstViewController

FirstViewController.m FirstViewController.m

#import "SecondViewController.h"

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    SecondViewController *secondViewController = (SecondViewController *)segue.destinationViewController;
    secondViewController.secondTFieldText = self.firstTField.text;
}

SecondViewController.m SecondViewController.m

#import "FirstViewController.h"

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.secondTField.text = self.secondTFieldText;
}

I hope this can help you 希望对您有所帮助

properties marked with IBOutlets are assigned when view controller's view is loading and presented to user. 在加载视图控制器的视图并将其呈现给用户时,将分配标记为IBOutlets的属性。

So it is null until you display your view controller somehow. 因此它为null,直到您以某种方式显示视图控制器为止。

In this code, you haven't added firstPage's view to anywhere yet, so it returns null. 在此代码中,您尚未将firstPage的视图添加到任何位置,因此它返回null。

- (void)viewDidLoad {
     [super viewDidLoad];
     FirstViewController *firstPage = [[FirstViewController alloc] init];
     _secondTField.text = firstPage.firstTField.text;
}

Your firstViewController's IBOutlets have not been initialized yet. 您的firstViewController的IBOutlets尚未初始化。 Create a string property. 创建一个字符串属性。 Set that. 设置那个。 Then in your firstViewController's viewDidLoad set the textField's text property. 然后在firstViewController的viewDidLoad中设置textField的text属性。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我尝试在另一个视图控制器中访问数据时,为什么它为null? (Objective-C,XCode) - Why is it null when I try to access data in another view controller? (Objective-C, XCode) 使用Objective-C类别时如何导入另一个视图控制器头文件? - How to import another view controller header file when I use Objective-C category? Objective-C以编程方式在另一个视图控制器上显示一个视图控制器 - Objective-C Showing a view controller on another view controller programmatically 将变量传递给Objective-C中嵌入在导航控制器中的View Controller - Pass variable to View Controller Embedded in Navigation Controller in Objective-C 如何在另一个视图控制器中控制对象? (Xcode:objective-c) - How to control objects in another view controller? (Xcode:objective-c) 在Objective-C中调用swift视图控制器文件的变量 - Calling to a variable of swift view controller file in Objective-C 当在objective-c中点击pin图时移动到另一个视图控制器 - Move to another view controller when the pin map tapped in objective-c 无法从 Objective-C 视图控制器访问 Swift var - iOS - Cannot access Swift var from Objective-C View controller - iOS 在我的Objective-C代码中间接调用时,为什么这个C数组为NULL? (cocos2d的) - Why is this C array NULL when indirectly called in my Objective-C code? (cocos2d) 从Swift的Objective-c基本视图控制器继承 - Inheritance from an Objective-c base view controller from Swift
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM