Я пытаюсь установить текст метки в сцене 2 из сцены 1. Я написал следующий код.Передача данных между сценами
// original viewController (scene 1)
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let destination = segue.destinationViewController as Scene2ViewController
destination.scene2Label.text = "Arrived from scene 1"
} //scene2Label is a Label in Scene2ViewController
Это вызывает ошибку: неожиданно найдено нуль при развертывании необязательного значения. Это poinitng до
destination.scene2Label.text = "Arrived from scene 1"
Как это возможно? Я думал, что я установил текстовое значение scene2Label в «Прибыл из сцены 1».
С другой стороны, этот код работать просто отлично:
class ViewController: UIViewController {
@IBOutlet weak var scene1Label: UILabel!
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let destination = segue.destinationViewController as Scene2ViewController
destination.labelText = "Arrived from scene 1"
}
}
class Scene2ViewController: UIViewController {
@IBOutlet weak var scene2Label: UILabel!
var labelText : String?
override func viewDidLoad() {
super.viewDidLoad()
scene2Label.text = labelText
}
}
Почему?