从UITableViewCell呈现UIAlertController

app*_*ing 15 uitableview uiviewcontroller ios swift

我正在尝试向自定义添加警报以UITableViewCell呈现UIAlertView我需要从中调用presentViewController UIViewController.但是,我不知道如何UIViewControllerUITableViewCell类中获取当前实例的访问权限.以下代码是我尝试使用扩展程序执行此操作.

我收到这个错误

表达式解析为未使用的函数

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 {
Run Code Online (Sandbox Code Playgroud)

Leo*_*Leo 38

您可以使用此扩展来查找显示单元格的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
}
}
Run Code Online (Sandbox Code Playgroud)

或用于rootViewController呈现:

rootViewController


Ban*_*ngs 14

尝试替换这行代码:

斯威夫特2

UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(refreshAlert, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)

斯威夫特3

UIApplication.shared.keyWindow?.rootViewController?.present(refreshAlert, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)


dul*_*gan 10

另一种方法是在表视图单元类中声明协议和委托属性,将控制器设置为单元委托,并在控制器中实现协议,该协议能够呈现警报视图控制器.

  • 我认为这种方法是最干净的。 (2认同)

Edo*_*ier 9

Leo的解决方案对我有用.我用它来自Custom集合视图单元格中呈现AlertView.

Swift 3.0的更新:

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
    }
}
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你.


reo*_*sed 5

您可以尝试使用协议和委托,使用此方法不仅可以显示警报,还可以让您与视图控制器进行更好的交互,以备将来需要时使用。例如:

protocol alerts: class{

    func presentAlert(title:String, message:String)

}
Run Code Online (Sandbox Code Playgroud)

然后在您的 tableViewCell 中,您将创建如下内容:

var alertsDelegate : alerts? 

//And whenever you need to present a message call something inside the cell like:

alertsDelegate?.presentAlert(title:"Your title", message:"Your message")
Run Code Online (Sandbox Code Playgroud)

然后在您的 viewController 中,首先将您的 viewController 设置为警报

class yourClassName : ViewController, alerts {

    //And add the functions of the protocol
    func presentAlert(title:String, message:String){
        //This is the function that'll be called from the cell
        //Here you can personalize how the alert will be displayed

    }
}
Run Code Online (Sandbox Code Playgroud)

其次,在“cellForRowAt”方法中设置单元格的委托

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "yourIdentifier") as! yourCellClass

    cell.alertsDelegate = self

}
Run Code Online (Sandbox Code Playgroud)

就像这样,您可以创建多个函数来适应您与 viewController 或 viewControllers 通信相关的需求。