Skip to content

Instantly share code, notes, and snippets.

@vikrant-Ios
Created December 1, 2017 09:31
Show Gist options
  • Save vikrant-Ios/02327007a4343f544aef46a4f94fc941 to your computer and use it in GitHub Desktop.
Save vikrant-Ios/02327007a4343f544aef46a4f94fc941 to your computer and use it in GitHub Desktop.
AlertExtension use in your project
//
// AlertExtension.swift
//
//
// Created by vikrant-Ios on 01/12/17.
// Copyright © 2017 vikrant-Ios. All rights reserved.
//
import Foundation
// Snippet
typealias AlertActionHandler = ((UIAlertAction) -> Void)
extension UIAlertControllerStyle {
func controller(title: String, message: String, actions: [UIAlertAction]) -> UIAlertController {
let aController = UIAlertController(
title: title,
message: message,
preferredStyle: self
)
actions.forEach { aController.addAction($0) }
return aController
}
}
extension String {
func alertAction(style: UIAlertActionStyle = .default, handler: AlertActionHandler? = nil) -> UIAlertAction {
return UIAlertAction(title: self, style: style, handler: handler)
}
}
// how to use alertcontroller in viewcontroler file
let alertController = UIAlertControllerStyle.alert.controller(
title: "Hi",
message: "Everyting will be fine.",
actions: [
"No".alertAction(),
"Yes".alertAction { _ in /* logic here */
print("action perform here")}
])
self.navigationController?.present(alertController, animated: true)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment