我想从一个视图控制器移动到另一个,并发送用户名:点击我得到斯威夫特赛格瑞准备 - 从一个视图控制器将数据发送到另一个
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 - 致命错误 –