Передача данных через segue через navigationController
Я пытаюсь перенести данные с одного viewController на другой. Я подключил viewController к другому контроллеру ViewController navigationController, а затем установил идентификатор "showItemSegue"
. Я получаю ошибку в этих двух строках:
var detailController = segue.destinationViewController as ShowItemViewController
detailController.currentId = nextId!
Изображение:
![enter image description here]()
Код:
override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
nextId = itemArray?.objectAtIndex(indexPath.row).objectForKey("objectId") as? NSString
self.performSegueWithIdentifier("showItemSegue", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if (segue.identifier == "showItemSegue") {
var detailController = segue.destinationViewController as ShowItemViewController
detailController.currentId = nextId!
}
}
Ответы
Ответ 1
Контроллер представления назначения для сегмента - UINavigationController
. Вам нужно попросить его, чтобы контроллер верхнего уровня дошел до реального контроллера представления:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showItemSegue" {
let navController = segue.destination as! UINavigationController
let detailController = navController.topViewController as! ShowItemViewController
detailController.currentId = nextId!
}
}