swift - 更改prepareforsegue中的标签

标签 swift

我在准备转场中编写快速代码时遇到问题。我编写的代码只是为了将文本框中的内容传输到另一个 View 上的标签,但是当我转到另一个页面时,它崩溃了。

var motionMovement : SecondViewController = segue.destinationViewController as SecondViewController   
motionMovement.laBel.text = teXt.text

然后我添加了这个以免使其崩溃

if segue.identifier == "hi"{
        var motionMovement : SecondViewController = segue.destinationViewController as SecondViewController
        motionMovement.laBel.text = teXt.text
    }

但现在它不会改变标签。

最佳答案

当您在 prepareForSegue: 中使用 socket 时, socket 并未被分配。您必须在 SecondViewController 中创建一个 String 变量来存储prepareForSegue: 中的标签文本,然后在 SecondViewControllerviewDidLoad 中将其设置为所需的 UILabel

SecondViewController

var labelString: String?
viewDidLoad() { 
     laBel.text = labelString
}

以及prepareForSegue

if segue.identifier == "hi"{
    var motionMovement : SecondViewController = segue.destinationViewController as SecondViewController
    motionMovement.labelString = teXt.text
}

关于swift - 更改prepareforsegue中的标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29422095/

相关文章:

swift - 如何设置单选按钮的背景不透明/透明?

ios - 如何在 Swift 中调用 Objective C UIView 的 initWithFrame

swift - ARKit – 球没有穿过 "SCNTorus"孔

swift - 检查用户是否点击了外部 View Controller

ios - 在 salesforcemobilesdk-ios 中使用 smartStore 时出错

ios - Kingfisher 默认缓存行为是什么?

ios - 上下移动 View 有限制

ios - 如何根据键对数组进行排序?

swift - iOS 10 上的快照 View 不包含图像

ios - swift 的 spritekit 声音设计