Created
December 1, 2017 09:31
-
-
Save vikrant-Ios/02327007a4343f544aef46a4f94fc941 to your computer and use it in GitHub Desktop.
AlertExtension use in your project
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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