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)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…