gpt4 book ai didi

ios - 从 UITableViewCell 呈现 UIAlertController

转载 作者:IT王子 更新时间:2023-10-29 05:19:57 24 4
gpt4 key购买 nike

我正在尝试向自定义 UITableViewCell 添加警报以呈现 UIAlertView 我需要从 UIViewController 调用 presentViewController。但是,我不知道如何从 UITableViewCell 类访问当前的 UIViewController 实例。下面的代码是我尝试使用扩展来做到这一点。

我收到这个错误

Expression resolved to unused function.

extension UIViewController
{

class func alertReminden(timeInterval: Int)
{
var refreshAlert = UIAlertController(title: "Refresh", message: "All data will be lost.", preferredStyle: UIAlertControllerStyle.Alert)

refreshAlert.addAction(UIAlertAction(title: "Ok", style: .Default, handler: { (action: UIAlertAction!) in
Alarm.createReminder("Catch the Bus",
timeInterval: NSDate(timeIntervalSinceNow: Double(timeInterval * 60)))
}))

refreshAlert.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: { (action: UIAlertAction!) in
println("Handle Cancel Logic here")
}))

UIViewController.presentViewController(refreshAlert)


}
}

class CustomRouteViewCell: UITableViewCell {

最佳答案

您可以使用此扩展来查找显示单元格的 viewController

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
}
}

或者使用rootViewController来呈现:

UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(refreshAlert, animated: true, completion: nil)

Swift 4.2 更新

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
}
}

或者使用rootViewController来呈现:

UIApplication.shared.keyWindow?.rootViewController?.present(alertVC, animated: true, completion: nil)

关于ios - 从 UITableViewCell 呈现 UIAlertController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30483104/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com