为什么我的变量在 iOS 应用程序中转换时为空? [英] Why are my variables empty when I cast them in my iOS application?

查看:25
本文介绍了为什么我的变量在 iOS 应用程序中转换时为空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 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:

  1. instantiateViewController(withIdentifier:) 的调用是否返回一个viewController?正如@aheze 指出的那样,您正在传递 EndConfiguration 的标识符.除非这是一个看起来很奇怪的字符串常量(例如 let EndConfiguration = "EndConfiguration").由于您试图将返回的对象强制转换为 EndConfiguration 类型,因此很明显有些地方不对劲.(EndConfiguration 不能既是视图控制器类又是字符串常量.)

    要调试对 instantiateViewController(withIdentifier:) 的调用,您应该得到摆脱演员表(as?EndConfiguration 位)并查看调用是否返回视图控制器.

  1. Does the call to instantiateViewController(withIdentifier:) return a viewController? As @aheze points out, you're passing an identifier of EndConfiguration. Unless that is a string constant (e.g. let EndConfiguration = "EndConfiguration") that seems odd. Since you're trying to cast the returned object to type EndConfiguration 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 to instantiateViewController(withIdentifier:) you should get rid of the cast (the as? EndConfiguration bit) and see if the call returns a view controller.

第二部分是条件转换(as?EndConfiguration 位.)如果创建的不是 EndConfiguration 类的视图控制器,它将失败.很容易不小心将视图控制器的类保留为默认类 UIViewController,而不是在界面构建器中将其更改为自定义类.要检查是否打开故事板,请选择该视图控制器的场景,选择该场景中的视图控制器,然后检查身份检查器".该类是您的 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.另外,您不应该操纵另一个视图控制器的视图.把它们当作私人的.您应该将一个字符串属性添加到 IT 在 vi​​ewWillAppear 中选取的目标视图控制器,并根据需要安装在它的视图中.

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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆