在所有视图控制器中创建警报功能 - 快速

时间:2017-04-12 08:08:23

标签: ios swift uiviewcontroller uialertview uialertcontroller

我正在尝试声明一个在我的应用中显示提醒的功能。为了避免重复工作,我试图为我的所有应用程序使用相同的功能。我尝试通过创建一个带有showNotification函数的类来做到这一点。但是当我创建该类的对象并调用该方法时,没有任何反应。我怎么能这样做?

class SharedPropertiesAndMetods : UIViewController {

    func showNotification(title: String, message: String)
    {
        let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
        let defaultAction = UIAlertAction(title: "تائید", style: .default, handler: nil)
        alertController.addAction(defaultAction)
        present(alertController, animated: true, completion: nil)
    }

}

7 个答案:

答案 0 :(得分:2)

实际上你可以在课堂外的任何地方声明一个简单的方法。

config.setConnectionTimeout(TimeUnit.MINUTES.toMillis(2L));

这种方式无论你在哪里调用它,你都会在完成句柄中按下ok按钮回调,甚至按照@Ganesh Kumar的说明制作扩展

答案 1 :(得分:2)

使用像这样的扩展程序

self.showAlert(title: "hi", message: "test")

像这样调用函数

{{1}}

答案 2 :(得分:2)

为什么不只是一个扩展

extension UIViewController {

    func showNotification(title: String, message: String)
    {
        let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
        let defaultAction = UIAlertAction(title: "تائید", style: .default, handler: nil)
        alertController.addAction(defaultAction)
        present(alertController, animated: true, completion: nil)
    }
}

答案 3 :(得分:2)

我要做的是创建一个'泛型'查看控制器来完成工作而不是从中继承:

<强> 1。如果您希望每次显示视图时都显示警报:

class GenericViewController: UIViewController {

    // MARK: - View lifecycle -

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        if let notification = self.shouldDisplayAlertNotification() {
            self.showNotification(notification)
        }
    }

    // MARK: - Internal methods -

    func shouldDisplayAlertNotification() -> AlertNotification? {
        return nil
    }

    // MARK: - Private methods -

    private func showNotification(_ alertNotification: AlertNotification) {
    }

}

class MyController: GenericViewController {

    override func shouldDisplayAlertNotification() -> AlertNotification? {
        return AlertNotification(title: "Title", message: "Message")
    }

}

AlertNotification是您的自定义模型类:

class AlertNotification {
    var title: String
    var message: String

    init(title: String, message: String) {
        self.title = title
        self.message = message
    }
}

这样,只有覆盖shouldDisplayAlertNotification的VC才会显示警告。

<强> 2。如果您想在&#39;要求&#39;

上显示提醒

根据建议,扩展UIViewController

extension UIViewController {
    func showNotification(title: String, message: String) {
    }
}

答案 4 :(得分:0)

您还可以在应用中创建一个util文件,因为您可以添加任何可重复使用的方法或功能,并在应用中的任何位置使用它,例如,

导入基金会 导入UIKit

// MARK: - ALERT

func showMessage(title:String,message:String!,VC:UIViewController){

let alert : UIAlertController = UIAlertController(title: "", message: message, preferredStyle: UIAlertControllerStyle.alert)
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) {
    UIAlertAction in
}
alert.addAction(okAction)
VC.present(alert, animated: true, completion: nil)

}

答案 5 :(得分:0)

您可以为alertController创建扩展,也可以选择操作处理程序。这将允许使用两个是否需要基于处理程序的不同Alert控制器。

extension  UIAlertControler {

class func genericErrorAlert(forAlert message: String, completion: ((UIAlertAction) -> Void)? = nil )

    let alert = UIAlertController(title: "Error", message: message, preferredStyle: .alert)

    alert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: completion))

    return alert
  }
}

答案 6 :(得分:0)

您可以使用此视图控制器扩展在整个应用程序中显示警报视图。 https://github.com/SumitKr88/UIViewController-ShowAlertView/blob/master/UIViewController%2BExtensions.swift

extension UIViewController {

/// Show alert view
/// - Parameter title: title of alert
/// - Parameter message: message of alert
/// - Parameter actionTitles: List of action button titles(ex : "OK","Cancel" etc)
/// - Parameter style: Style of the buttons
/// - Parameter actions: actions repective to each actionTitles
/// - Parameter preferredActionIndex: Index of the button that need to be shown in bold. If nil is passed then it takes cancel as default button.

/**
 Example usage:-
 Just make sure actionTitles and actions array the same count.

 /********** 1. Pass nil if you don't need any action handler closure. **************/
 self.showAlert(title: "Title", message: "message", actionTitles: ["OK"], style: [.deafult], actions: [nil])

 /*********** 2. Alert view with one action **************/

 ///     let okActionHandler: ((UIAlertAction) -> Void) = {(action) in
 //Perform action of Ok here
 }
 self.showAlert(title: "Title", message: "message", actionTitles: ["OK", "CANCEL"], style: [.default, .cancel], actions: [okayActionHandler, nil])

 /********** 3.Alert view with two actions **************/

 let okActionHandler: ((UIAlertAction) -> Void) = {(action) in
 //Perform action of ok here
 }

 let cancelActionHandler: ((UIAlertAction) -> Void) = {(action) in
 //Perform action of cancel here
 }

 self.showAlert(title: "Title", message: "message", actionTitles: ["OK", "CANCEL"], style: [.default, .cancel], actions: [okActionHandler,cancelActionHandler], preferredActionIndex: 1)
 */

public func showAlert(title: String?,
                      message: String?,
                      actionTitles: [String?],
                      style: [UIAlertAction.Style],
                      actions: [((UIAlertAction) -> Void)?],
                      preferredActionIndex: Int? = nil) {
    let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
    for (index, title) in actionTitles.enumerated() {
        let action = UIAlertAction(title: title, style: style[index], handler: actions[index])
        alert.addAction(action)
    }
    if let preferredActionIndex = preferredActionIndex { alert.preferredAction = alert.actions[preferredActionIndex] }
    self.present(alert, animated: true, completion: nil)
}}