简体   繁体   English

当我尝试在另一个视图控制器中访问数据时,为什么它为null? (Objective-C,XCode)

[英]Why is it null when I try to access data in another view controller? (Objective-C, XCode)

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 firstPage.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,但是即使我打印了第二个视图控制器,我的firstPage.firstTField.text还是(null) _firstTField.text在我的第一个视图控制器中,它打印出了输入的输入。

What may be the problem? 可能是什么问题? Why is null? 为什么为空?

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;
}

You should treat a view controller's views as private. 您应该将视图控制器的视图视为私有。 That's part of the appearance of the view controller, not it's function, and if objects outside of the view controller expect the views to look a certain way then when you change the appearance of the view controller's views it breaks other code. 那是视图控制器外观的一部分,而不是它的功能,并且如果视图控制器外部的对象期望视图以某种方式看起来,那么当您更改视图控制器视图的外观时,它将破坏其他代码。 Bad. 坏。

In this situation it's worse. 在这种情况下,情况更糟。 It just doesn't work, for the reason @nhgrif explains. 只是不起作用,原因是@nhgrif解释。 You just created a new FirstViewController object, and it's views don't even exist yet. 您刚刚创建了一个新的FirstViewController对象,它的视图甚至还不存在。 (A view controllers views are not created until the system is asked to display the view controller to the screen.) (在要求系统在屏幕上显示视图控制器之前,不会创建视图控制器视图。)

You should create a property in your view controller that exposes the string(s) you need to read/write and use that instead. 您应该在视图控制器中创建一个属性,该属性公开需要读取/写入的字符串,并改为使用该字符串。

However it's strange that you would create a new instance of a view controller and then immediately try to read text from one of it's fields. 但是奇怪的是,您将创建视图控制器的新实例,然后立即尝试从其中一个字段读取文本。 How can it possibly have useful data if the view controller was created on the line before? 如果视图控制器是在之前的行上创建的,它怎么可能会有有用的数据? What are you expecting to happen? 您期望发生什么?

暂无
暂无

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

相关问题 当我尝试从另一个视图控制器(Objective-c?)访问变量时,为什么变量为null? - Why is my variable null when I am attempting to access it from another view controller (Objective-c? 如何在另一个视图控制器中控制对象? (Xcode:objective-c) - How to control objects in another view controller? (Xcode:objective-c) 使用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 ios / xcode / objective-c:有什么方法可以链接到核心数据驱动页面中的其他视图控制器? - ios/xcode/objective-c: Any way to link to other view controller in core-data driven page? 当在objective-c中点击pin图时移动到另一个视图控制器 - Move to another view controller when the pin map tapped in objective-c Objective-C:关闭视图控制器时保持活动状态吗? - Objective-C : Keep a View Controller active when dismissed? 如何在objective-c中识别模型,视图和控制器? - How do I identify a model, view and controller in objective-c? 在Objective-c中将数据从一个viewController传递到另一个viewController时,我得到了空值 - I'm getting null while passing data from one viewController to another in objective-c ios / xcode / objective-c:导航到嵌入在导航控制器中的模态视图控制器 - ios/xcode/objective-c: Segue to modal view controller embedded in navigation controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM