I have a button in my UICollectionViewCell.swift: I want, based on certain parameters for it to be able to present an alert.
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)
}
On the self.collectionView!.presentViewController
line I get a break:
fatal error: unexpectedly found nil while unwrapping an Optional value
I'm guessing that this has someting to do with how CollectionView
is being used - I don't totally understand optionals yet. I know that a UICollectionCell
can't .presentViewController
- which is why I'm trying to get UICOllectionView
to do it.
How do I make this work? I've thought of using an extension
but don't know how to make UICOllectionViewCell
adopt .presentViewController
Any ideas?