【发布时间】:2016-02-21 03:07:13
【问题描述】:
我的 UICollectionViewCell.swift 中有一个按钮:我希望它能够根据某些参数显示警报。
class CollectionViewCell: UICollectionViewCell {
var collectionView: CollectionView!
@IBAction func buttonPressed(sender: UIButton) {
doThisOrThat()
}
func doThisOrThat() {
if x == .NotDetermined {
y.doSomething()
} else if x == .Denied || x == .Restricted {
showAlert("Error", theMessage: "there's an error here")
return
}
}
func showAlert(title: String, theMessage: String) {
let alert = UIAlertController(title: title, message: theMessage, preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
self.collectionView!.presentViewController(alert, animated: true, completion: nil)
}
在self.collectionView!.presentViewController 行我休息一下:
fatal error: unexpectedly found nil while unwrapping an Optional value
我猜这与 CollectionView 的使用方式有关 - 我还不完全了解可选选项。我知道UICollectionCell 不能.presentViewController - 这就是为什么我要让UICOllectionView 去做。
我该如何进行这项工作?我曾想过使用extension,但不知道如何让UICOllectionViewCell 采用.presentViewController
有什么想法吗?
【问题讨论】:
标签: ios uibutton uicollectionview uicollectionviewcell uialertcontroller