我想從一個視圖控制器移動到另一個,併發送用戶名:點擊我得到斯威夫特賽格瑞準備 - 從一個視圖控制器將數據發送到另一個
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
self.performSegueWithIdentifier("chosenPerson", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "chosenPerson" {
let chosenPerson = segue.destinationViewController as? chosenPersonViewController
let indexPaths = self.collectionView!.indexPathsForSelectedItems()!
let indexPath = indexPaths[0] as NSIndexPath
chosenPerson!.userID = self.usersArray[indexPath.row].userId
}
:「致命錯誤:意外發現零,而展開一個可選值「 我做錯了什麼?
哪一行運行中斷? –
只需幾個指示即可開始。嘗試逐行瀏覽你的'if語句'中的行,以瞭解正在發生的事情。潛在的失敗候選者是'self.collectionView!.indexPathsForSelectedItems()!'和'chosenPerson!.userID = self.usersArray [indexPath.row] .userId'因此試圖證明'indexPathsForSelectedItems()'返回一些有用的東西,並且你的位置'indexPath.row'上的usersArray實際上包含了一些東西。 – pbodsk
chosenPerson!.userID = self.usersArray [indexPath.row] .userId - 致命錯誤 –