3

I want to create a reusable UIAlertController class for my project.I tried the following code.But my alert is not showing.What is the wrong in my code or any code help.This is my alert class

class AlertView: NSObject {

 class func showAlert(view: UIViewController , message: String){

        let alert = UIAlertController(title: "Warning", message: message, preferredStyle: UIAlertControllerStyle.Alert)
        alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
        view.presentViewController(alert, animated: true, completion: nil)
    }
}

and i am calling this in my another view controller like

class ViewController: UIViewController {

  override func viewDidLoad() {
      super.viewDidLoad()
      AlertView.showAlert(self, message: "Test alert")
      // Do any additional setup after loading the view, typically from a nib.
   }
}
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
Madhumitha
  • 3,794
  • 8
  • 30
  • 45

3 Answers3

8

You can use Anbu Karthiks answer. As an alternative I use Swift 2s protocol extensions to show alerts.

import UIKit

protocol Alertable { }
extension Alertable where Self: UIViewController {

func showAlert(title title: String, message: String) {

    let alertController = UIAlertController(title: title, message: message, preferredStyle: .Alert)

    let okAction = UIAlertAction(title: "OK", style: .Cancel) { _ in }
    alertController.addAction(okAction)

    DispatchQueue.main.async {       
       self.view?.window?.rootViewController?.presentViewController(alertController, animated: true, completion: nil)
    }
}

 func showAlertWithSettings(title title: String, message: String) {

    let alertController = UIAlertController(title: title, message: message, preferredStyle: .Alert)

    let okAction = UIAlertAction(title: "OK", style: .Cancel) { _ in }
    alertController.addAction(okAction)

    let settingsAction = UIAlertAction(title: "Settings", style: .Default) { _ in
       guard let url = NSURL(string: UIApplicationOpenSettingsURLString) else { return }
        UIApplication.sharedApplication().openURL(url)
    }
    alertController.addAction(settingsAction)

    DispatchQueue.main.async {       
         self.view?.window?.rootViewController?.presentViewController(alertController, animated: true, completion: nil)
   }
}

Now in your viewControllers where you need to show alerts you simply conform to the protocol

class ViewController: UIViewController, Alertable { } 

and call the methods like

showAlert(title: "Alert title", message: "Alert message")

as if they are part of the viewController itself.

Swift SpriteKit: Best practice to access UIViewController in GameScene

Note: As members have said in the comments, viewDidLoad might be to soon to show an alert. Try using a slight delay or ViewDidAppear

Hope this helps

crashoverride777
  • 10,581
  • 2
  • 32
  • 56
4

present your alert in main_queue

class AlertView: NSObject {

    class func showAlert(view: UIViewController , message: String) {
        let alert = UIAlertController(title: "Warning", message: message, preferredStyle: UIAlertControllerStyle.Alert)
        alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))

        dispatch_async(dispatch_get_main_queue(), {
            view.presentViewController(alert, animated: true, completion: nil)
        })
    }
}

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        AlertView.showAlert(self, message: "Test alert")
        // Do any additional setup after loading the view, typically from a nib.
    }
}
Shebuka
  • 3,148
  • 1
  • 26
  • 43
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
0

REUSABLE ALERT in swift 3&4

class AlertView: NSObject {
    class func showAlert(view: UIViewController , title: String , message: String){
        let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
        alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
        view.present(alert, animated: true, completion: nil)
    }
}

 //How to use?
 //Alert.showAlert(view: self, title: "Alert!", message: "Reusable Alert")
Sai kumar Reddy
  • 1,751
  • 20
  • 23