Однажды у меня была эта ошибка, когда я пытался установить свои значения Outlets из метода подготовки к переходу следующим образом:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destination = segue.destination as? DestinationVC{
if let item = sender as? DataItem{
// This line pops up the error
destination.nameLabel.text = item.name
}
}
}
Затем я обнаружил, что не могу установить значения пункта назначения. выходы контроллера, потому что контроллер еще не загружен или не инициализирован.
Итак, я решил это следующим образом:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destination = segue.destination as? DestinationVC{
if let item = sender as? DataItem{
// Created this method in the destination Controller to update its outlets after it's being initialized and loaded
destination.updateView(itemData: item)
}
}
}
Конечный контроллер:
// This variable to hold the data received to update the Label text after the VIEW DID LOAD
var name = ""
// Outlets
@IBOutlet weak var nameLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
nameLabel.text = name
}
func updateView(itemDate: ObjectModel) {
name = itemDate.name
}
Надеюсь, этот ответ поможет кому-то с такой же проблемой, как я нашел отмеченный ответ: отличный ресурс для понимания опциональных устройств и того, как они работают, но сам этот вопрос не решен напрямую.
Вы смотрели Завод программного обеспечения веб-сервисов ? Это позволяет вам полностью контролировать создание прокси-кода.