Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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)
- }
- }
- 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.
- }
- }
- 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.
- }
- }
- import UIKit
- protocol AlertController { }
- extension AlertController 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)
- 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)
- view?.window?.rootViewController?.presentViewController(alertController, animated: true, completion: nil)
- }
- }
- class ViewController: UIViewController, AlertController { }
- showAlert(title: "Alert title", message: "Alert message")
- 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👍🏿")
Add Comment
Please, Sign In to add comment