UIAlertView与Swift 4中的return bool共享的类

问题描述 投票:0回答:1
protocol AlertDelegate: AnyObject {
      func didGetResponse(text: String?)
}
class AlertClass{

    weak var delegate: AlertDelegate?
    static let sharedInstance = AlertClass()
    //Show alert
    func alertWindow(title: String, message: String) {
        DispatchQueue.main.async(execute: {
            let alertWindow = UIWindow(frame: UIScreen.main.bounds)
            alertWindow.rootViewController = UIViewController()
            alertWindow.windowLevel = UIWindow.Level.alert + 1

            let alert2 = UIAlertController(title: title, message: message, preferredStyle: .alert)
            let defaultAction1 = UIAlertAction(title: "Yes", style: .destructive, handler: { action in
                print("Yes")
            })
            let defaultAction2 = UIAlertAction(title: "No", style: .destructive, handler: { action in
                print("No")
            })
            alert2.addAction(defaultAction1)
            alert2.addAction(defaultAction2)

            alertWindow.makeKeyAndVisible()

            alertWindow.rootViewController?.present(alert2, animated: true, completion: nil)



        })
    }
}

如何在此类中返回布尔值?

ios swift uialertview
1个回答
0
投票

`

class AlertClass{//This is shared class

    weak var delegate: AlertDelegate?
    static let sharedInstance = AlertClass()

    //Show alert
    func alertWindow(title: String, message: String, completion:@escaping (_ result: Bool) -> Void) {
            let alertWindow = UIWindow(frame: UIScreen.main.bounds)
            alertWindow.rootViewController = UIViewController()
            alertWindow.windowLevel = UIWindow.Level.alert + 1

            let alert2 = UIAlertController(title: title, message: message, preferredStyle: .alert)
            let defaultAction1 = UIAlertAction(title: "Yes", style: .destructive, handler: { action in
                completion(true)

            })
            let defaultAction2 = UIAlertAction(title: "No", style: .destructive, handler: { action in
                print("No")
                completion(false)
            })
            alert2.addAction(defaultAction1)
            alert2.addAction(defaultAction2)

            alertWindow.makeKeyAndVisible()

            alertWindow.rootViewController?.present(alert2, animated: true, completion: nil)
    }
}
alert.alertWindow(title: "", message: "Checking Completion") { (result) in
            if result{
                print("It's yes")
            }
            else {
                print("It's no")
            }
        }

`我在@rmaddy解释中使用这种方法,是正确的方法吗?

© www.soinside.com 2019 - 2024. All rights reserved.