create alert function in all view controllers - swift

被刻印的时光 ゝ 提交于 2019-12-01 00:47:37

What I would do is to create a 'generic' view controller that do the job and than inherit from it:

1. If you want to display alert each time view did appear:

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

}

Where AlertNotification is your custom model class:

class AlertNotification {
    var title: String
    var message: String

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

In this way, only VC that overrides shouldDisplayAlertNotificationwill display alert.

2. If you want to display alert on 'demand':

As suggested, extend UIViewController

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

Actually you can declare a simple method anywhere outside class.

func showAlertWithCompletion(message:String,okTitle:String,cancelTitle:String?,completionBlock:@escaping (_ okPressed:Bool)->()){
    let alertController = UIAlertController(title: AppName, message: message, preferredStyle: .alert)
    let okAction = UIAlertAction(title: okTitle, style: .default) { (ok) in
        completionBlock(true)
    }
    alertController.addAction(okAction)
    if let cancelTitle = cancelTitle{
        let cancelOption = UIAlertAction(title: cancelTitle, style: .cancel, handler: { (axn) in
            completionBlock(false)

        })
        alertController.addAction(cancelOption)
    }

    if let topController = UIWindow.topViewController(){
      topController.present(alertController, animated: true, completion: nil)
    }

}

This way wherever you call it, you will get ok button pressed callback in completion handle or even make Extension as described by @Ganesh Kumar

Use an extension like this

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

call the function like this

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

Why not just an extension

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

You could create extension to alertController and also have option for action handler. This will allow to use two different Alert controller based on handler is required or not.

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

You can also create one util file in your app, in that you can add any reusable method or function and use it anywhere in your app Like,

import Foundation import 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)

}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!