[英]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?
有人可以指导我如何解决/解决这个问题吗?
@property (weak, nonatomic) IBOutlet UITextField *firstTField;
@property (weak, nonatomic) IBOutlet UITextField *secondTField;
#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.