在全局swift文件中添加alertcontroller吗?

时间:2018-11-02 07:47:00

标签: ios swift

我正在尝试在除UIViewController文件之外的swift文件内创建一个警告框。但我无法创建它。

extension NetworkManager {

    func showAlert(message: String,from:UIViewController, title: String = "") {
        let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
        let OKAction = UIAlertAction(title: "OK", style: .default, handler: nil)
        alertController.addAction(OKAction)
        from.present(alertController, animated: true, completion: nil)
    }
}

上面的代码用于实现alertcontroller,但是我不知道如何传递需要显示的视图控制器,因此需要帮助。

4 个答案:

答案 0 :(得分:2)

extension UIViewController {
     func showAlert(message: String, title: String = "") {
        let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
        let OKAction = UIAlertAction(title: "OK", style: .default, handler: nil)
        alertController.addAction(OKAction)
        self.present(alertController, animated: true, completion: nil)
    }    
}

并像这样使用

from.showAlert(message:"Your message", title: "Title")

答案 1 :(得分:1)

在您的项目中添加一个Utilities类。

class Utilities {

    static func showSimpleAlert(OnViewController vc: UIViewController, Message message: String) {

        //Create alertController object with specific message
        let alertController = UIAlertController(title: "App Name", message: message, preferredStyle: .alert)

        //Add OK button to alert and dismiss it on action
        let alertAction = UIAlertAction(title: "OK", style: .default) { (action) in

            alertController.dismiss(animated: true, completion: nil)
        }
        alertController.addAction(alertAction)

        //Show alert to user
        vc.present(alertController, animated: true, completion: nil)
    }
}

用法:

Utilities.showSimpleAlert(OnViewController: self, Message: "Some message")

答案 2 :(得分:1)

这是我所做的扩展。它允许显示警报操作表,并允许“从框中”执行多个操作

extension UIViewController {

    func presentAlert(title: String?, message: String, actions: UIAlertAction..., animated: Bool = true) {
        let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
        actions.forEach { alert.addAction($0) }
        self.present(alert, animated: animated, completion: nil)
    }

    func presentActionSheet(title: String?, message: String, actions: UIAlertAction..., animated: Bool = true) {
        let alert = UIAlertController(title: title, message: message, preferredStyle: .actionSheet)
        actions.forEach { alert.addAction($0) }
        self.present(alert, animated: animated, completion: nil)
    }
}

用法

    let delete = UIAlertAction(title: "Delete", style: .destructive, handler: { _ in /* Your code here */})
    let cancel = UIAlertAction(title: "Cancel", style: .default, handler: nil)

    presentAlert(title: .albumPreferencesDeleteAlertTitle, message: "Very important message", actions: delete, cancel)

答案 3 :(得分:0)

这是在视图控制器上显示警报的更通用的方法

func showAlert(msg: String, inViewController vc: UIViewController, actions: [UIAlertAction]? = nil, type: UIAlertControllerStyle = .alert, title: String = kAppName) {

    let alertType: UIAlertControllerStyle = .alert
    let alertTitle = kAppName
    let alertVC = UIAlertController(title: alertTitle, message: msg, preferredStyle: alertType)

    if let actions = actions {

        for action in actions {
            alertVC.addAction(action)
        }

    } else {

        let actionCancel = UIAlertAction(title: "OK", style: .cancel, handler: nil)
        alertVC.addAction(actionCancel)
    }

    vc.present(alertVC, animated: true, completion: nil)
}

用法

AppUtilities.showAlert(msg: "Test msg", inViewController: self) //for alert
AppUtilities.showAlert(msg: "Test msg", inViewController: self, actions: [okAction, cancelAction]) //for alert
AppUtilities.showAlert(msg: "Test Msg", inViewController: self, type: .actionSheet) //shows action sheet

您可以在扩展中添加此功能,也可以根据需要创建单独的实用程序类。