当我在iOS应用程序中将变量强制转换时,为什么变量为空? [英] Why are my variables empty when I cast them in my iOS application?
问题描述
我在Xcode上为IOS编写应用程序,我想将变量发送到另一个ViewController.问题是,当我想从名为"Step1" 的控制器发送来设置我的第二个视图控制器名为"Step2" 的标签时.为此,我在第1步" 中以这种方式进行操作:
Hi, I am coding an application on Xcode for IOS and I would like to send a variable to another ViewController. The problem is that when I want to send from the controller called "Step1" to set the label of my second view controller called "Step2". To do this I proceed in this way in "Step 1":
guard let step2 = self.storyboard?.instantiateViewController(withIdentifier: Step2) as? EndConfiguration else
{
fatalError("Error when trying to get the reference to the Step 2")
}
step2.nameLabelOutlet?.text = "Jonh"
但是我的标签没有设置...有人可以帮我解决这个问题吗?
But my label is not set... Could someone help me with this problem?
谢谢.
推荐答案
有3个部分可以调试问题:
EDITED:
There are 3 parts to debugging the problem:
-
对
instantiateViewController(withIdentifier:)
的调用是否返回viewController?正如@aheze指出的那样,您正在传递标识符EndConfiguration
.除非那是一个字符串常量(例如let EndConfiguration = "EndConfiguration"
),否则看起来很奇怪.由于您尝试将返回的对象强制转换为EndConfiguration
类型,因此似乎很不对劲. (EndConfiguration
不能同时是视图控制器类和字符串常量.)
要调试对instantiateViewController(withIdentifier:)
的调用,您应该删除强制转换(as? EndConfiguration
位),然后查看是否调用返回一个视图控制器.
Does the call to
instantiateViewController(withIdentifier:)
return a viewController? As @aheze points out, you're passing an identifier ofEndConfiguration
. Unless that is a string constant (e.g.let EndConfiguration = "EndConfiguration"
) that seems odd. Since you're trying to cast the returned object to typeEndConfiguration
it seems clear that something isn't right. (EndConfiguration
can't be both a view controller class and a string constant.)
To debug the call toinstantiateViewController(withIdentifier:)
you should get rid of the cast (theas? EndConfiguration
bit) and see if the call returns a view controller.
第二部分是条件强制转换(as? EndConfiguration
位.)如果创建的不是类EndConfiguration
的视图控制器,则它将失败.意外地将视图控制器的类保留为默认类UIViewController
,而不在Interface builder中将其更改为自定义类是很容易的.要检查是否打开了故事板,请为该视图控制器选择场景,在该场景中选择视图控制器,然后在身份检查器"中签入.该类是您的EndConfiguration
类.
The second part is the conditional cast (The as? EndConfiguration
bit.) If what is created is not a view controller of class EndConfiguration
that will fail. It's pretty easy to accidentally leave your view controller's class as the default class UIViewController
, and not change it to your custom class in Interface builder. To check that open your storyboard, select the scene for that view controller, select the view controller in that scene, and check in the "identity inspector" that the class is your EndConfiguration
class.
正如PaulW在其评论中指出的那样,视图控制器的视图在调用instantiateViewController(withIdentifier:)
时返回时尚未加载,因此您的代码step2.nameLabelOutlet?.text = "Jonh"
将不起作用,因为nameLabelOutlet将为nil .另外,您不应操纵另一个视图控制器的视图.视他们为私人.您应该在目标View Controller中添加一个字符串属性,IT会在IT中将其从viewWillAppear中拾取,并适当地安装在其视图中.
As pointed out by PaulW in his comment, a view controller's views are not loaded by the time it's returned in a call to instantiateViewController(withIdentifier:)
, so your code step2.nameLabelOutlet?.text = "Jonh"
won't work because nameLabelOutlet will be nil. Plus, you shouldn't manipulate another view controller's views. Treat them as private. You should add a string property to the target view controller that IT picks up in viewWillAppear and installs in it's view as appropriate.
这篇关于当我在iOS应用程序中将变量强制转换时,为什么变量为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!