在prepareforsegue中更改标签

问题描述:

我在编写segue内部编写swift代码时遇到了问题。我编写的代码只是为了将我的文本框内的文字转换为另一个视图上的标签,但是当我转到其他页面时,这会崩溃。在prepareforsegue中更改标签

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 
    } 

但现在不会更改标签。

+0

它在哪里崩溃的另一页?什么行代码? – 2015-04-03 00:53:47

当你prepareForSegue:使用它。你必须在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 
} 
+0

现在我已经做到了,无论如何它都会将标签空白。 – tom3377 2015-04-02 21:05:44

+0

@ tom3377,根据我的经验,赋值应该在'viewWillAppear'而不是'viewDidLoad'中。希望这仍然可以帮助你 – MoonKnight 2016-06-28 03:28:17