2015-04-02 86 views
0

我在編寫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

回答

1

當你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