Presenting UIAlertController from UITableViewCell
Try to replace to this line of code:
Swift 2
UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(refreshAlert, animated: true, completion: nil)
Swift 3
UIApplication.shared.keyWindow?.rootViewController?.present(refreshAlert, animated: true, completion: nil)
You can use this extension to find the viewController that present the cell
extension UIView {
var parentViewController: UIViewController? {
var parentResponder: UIResponder? = self
while parentResponder != nil {
parentResponder = parentResponder!.nextResponder()
if parentResponder is UIViewController {
return parentResponder as! UIViewController!
}
}
return nil
}
}
Or use rootViewController
to Present:
UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(refreshAlert, animated: true, completion: nil)
Swift 4.2 Update
extension UIView {
var parentViewController: UIViewController? {
var parentResponder: UIResponder? = self
while parentResponder != nil {
parentResponder = parentResponder!.next
if parentResponder is UIViewController {
return parentResponder as? UIViewController
}
}
return nil
}
}
Or use rootViewController
to Present:
UIApplication.shared.keyWindow?.rootViewController?.present(alertVC, animated: true, completion: nil)