我在准备转场中编写快速代码时遇到问题。我编写的代码只是为了将文本框中的内容传输到另一个 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:
中的标签文本,然后在 SecondViewController
的 viewDidLoad
中将其设置为所需的 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/