我是 swift 的新手,我遇到了这个错误。我有一个 viewController,它有一个 Collection View ,其中有 Storyboard segue 到 viewContoller 和两个按钮,这些按钮将 segue 推送到其他 viewController。当我尝试单击这些按钮时,出现 fatal error ,表明发送者不是具有以下代码的 collectionView。
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath)
{
if let cell = collectionViewOne.cellForItemAtIndexPath(indexPath)
{
performSegueWithIdentifier("showDeals", sender: cell)
}
else
{
// Error indexPath is not on screen: this should never happen.
}
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
assert(sender as? UICollectionViewCell != nil, "sender is not a collection view")
if let indexPath = self.collectionViewOne?.indexPathForCell(sender as! UICollectionViewCell) {
if segue.identifier == "showDeals" {
let detailVC: DealsTableViewController = segue.destinationViewController as! DealsTableViewController
detailVC.someCategory = MyIds[indexPath.row]
}
} else {
// Error sender is not a cell or cell is not in collectionView.
}
}
最佳答案
尝试添加
print("sender type : \(sender.dynamicType)")
在prepareForSegue中,不要使用断言,而是使用可选链接来转换发送者变量:
if let cell = sender as? UICollectionViewCell
而不是:
assert(sender as? UICollectionViewCell != nil, "sender is not a collection view")
关于ios - 在具有 Storyboard segue 的 collectionviewcell super View 上带有推送 Segue 的按钮会引发错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33869138/