2014-11-15 4 views
33

Я пытаюсь перенести данные с одного viewController на другой. Я подключил viewController к другому диспетчеру навигации ViewController, а затем установил идентификатор в "showItemSegue". Я получаю сообщение об ошибке в этих двух строках: иллюстрацияПередача данных с помощью segue через navigationController

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! 
    } 

} 
+0

Пожалуйста отправьте сообщение об ошибке полного –

ответ

62

Адресат вид контроллер Segue является 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! 
    } 
} 
+0

работает как шарм! Спасибо друг! после нескольких часов он спас меня несколько часов :) – datayeah

+0

Спасибо, это решение работает отлично с быстрым 3 – mic

Смежные вопросы