Gábor Csontos
Gábor Csontos

Reputation: 33

How to present an AlertView from a UICollectionViewCell

I am using a UICollectionView with a Map in the header.

I want to handle Core Location errors. I have 3 error types and for two of them I want to present a UIAlertView.

But I am getting an error, because UICollectionViewCell doesn't have a member called presentViewController.

func locationUnknown() {
    var alert = UIAlertController(title: "Location Unknown", message: "Please try again later.", preferredStyle: UIAlertControllerStyle.Alert)

    let alertAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: { (action: UIAlertAction!) -> Void in

    })
    alert.addAction(alertAction)
    self.presentViewController(alert, animated:true, completion: nil)
}

How can I send this UIAlertView to the UICollectionView's screen?

Upvotes: 3

Views: 1696

Answers (1)

pegli
pegli

Reputation: 690

Use the window's root view controller to present the alert:

self.window?.rootViewController?.present(alert, animated: true, completion: nil)

Upvotes: 12

Related Questions