I have this code, but I dont know how to show a textfield inside the UIAlertView.
var altMessage = UIAlertController(title: \"Warning\", message: \"This is A
public func alertWithTextField(title: String? = nil, message: String? = nil, placeholder: String? = nil, completion: @escaping ((String) -> Void) = { _ in }) {
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
alert.addTextField() { newTextField in
newTextField.placeholder = placeholder
}
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel) { _ in completion("") })
alert.addAction(UIAlertAction(title: "Ok", style: .default) { action in
if
let textFields = alert.textFields,
let tf = textFields.first,
let result = tf.text
{ completion(result) }
else
{ completion("") }
})
navigationController?.present(alert, animated: true)
}
Usage:
alertWithTextField(title: "bork", message: "borkborkbork", placeholder: "bork?") { result in
print(result)
}
Swift 2.2
import UIKit
extension UIAlertController {
// MARK: - UIAlertController+Present
private struct ButtonsName {
static let Ok = NSLocalizedString("uIAlertController.buttonName.ok", comment: "")
static let Cancel = NSLocalizedString("uIAlertController.buttonName.cancel", comment: "")
}
class func suggestionAlertViewWithTitle(title:String?, placeholder:String, message:String, presenter:UIViewController, destructive:Bool = false,
okButtonCompletion:((enteredSuggestion:String?)->Void)?, cancelButtonCompletion:(()->Void)?, presentCompletion:(()->Void)?) {
var alertTitle = UIAlertController.appName()
if let userTitle = title {
alertTitle = userTitle
}
let controller = UIAlertController(title: alertTitle, message: message, preferredStyle: .Alert)
let okAction = UIAlertAction(title: ButtonsName.Ok, style: destructive == true ? .Destructive : .Default) { (action) in
if let okButtonCompletion = okButtonCompletion {
let text = controller.textFields?.first?.text
dispatch_async(dispatch_get_main_queue(), {
okButtonCompletion(enteredSuggestion: text)
})
}
}
let cancelAction = UIAlertAction(title: ButtonsName.Cancel, style: .Cancel) { (action) in
if let cancelButtonCompletion = cancelButtonCompletion {
dispatch_async(dispatch_get_main_queue(), {
cancelButtonCompletion()
})
}
}
controller.addAction(okAction)
controller.addAction(cancelAction)
controller.addTextFieldWithConfigurationHandler { (textField) in
textField.placeholder = placeholder
}
dispatch_async(dispatch_get_main_queue(), {
presenter.presentViewController(controller, animated: true, completion: presentCompletion)
})
}
// MARK: - Private
private static func appName () -> String {
return NSBundle.mainBundle().infoDictionary!["CFBundleName"] as! String
}
}
usage:
UIAlertController.suggestionAlertViewWithTitle(nil, placeholder: placeholder, message: message,
presenter: self, destructive: false,
okButtonCompletion: { (enteredSuggestion) in
self.logger.sendAllLogs(self.currentUser, suggestedTitle: enteredSuggestion)
}, cancelButtonCompletion:nil, presentCompletion: nil)
a little bit overloaded, but u can always make some parameters optional or/and default
Updated for swift 3 :
used below simple code:
func showAlertWithTwoTextFields() {
let alertController = UIAlertController(title: "Add Event", message: "Enter event and it's description", preferredStyle: .alert)
let saveAction = UIAlertAction(title: "Save", style: .default, handler: {
alert -> Void in
let eventNameTextField = alertController.textFields![0] as UITextField
let descriptionTextField = alertController.textFields![1] as UITextField
print("firstName \(String(describing: eventNameTextField.text)), secondName \(String(describing: descriptionTextField.text))")
if eventNameTextField.text != "" || descriptionTextField.text != ""{
}else{
// self.showAlertMessageToUser(title: "Alert", messageToUser: "Fields should not be empty, Please enter given info...")
// Show Alert Message to User As per you want
}
})
let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: {
(action : UIAlertAction!) -> Void in
})
alertController.addTextField { (textField : UITextField!) -> Void in
textField.placeholder = "Enter event Name"
}
alertController.addTextField { (textField : UITextField!) -> Void in
textField.placeholder = "Enter event description in short"
}
alertController.addAction(saveAction)
alertController.addAction(cancelAction)
self.present(alertController, animated: true, completion: nil)
}
// Enjoy Coding...!
In Swift 3
let alert = UIAlertController(title: "Alert Ttitle", message: "Alert Message", preferredStyle:
UIAlertControllerStyle.alert)
alert.addTextField(configurationHandler: textFieldHandler)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler:{ (UIAlertAction)in
}))
self.present(alert, animated: true, completion:nil)
func textFieldHandler(textField: UITextField!)
{
if (textField) != nil {
textField.text = "Filename"
}
}
Try This Code (with swift):
func configurationTextField(textField: UITextField!)
{
println("configurat hire the TextField")
if let tField = textField {
self.textField = textField! //Save reference to the UITextField
self.textField.text = "Hello world"
}
}
func handleCancel(alertView: UIAlertAction!)
{
println("User click Cancel button")
println(self.textField.text)
}
var alert = UIAlertController(title: "Alert Title", message: "Alert Message", preferredStyle: UIAlertControllerStyle.Alert)
alert.addTextFieldWithConfigurationHandler(configurationTextField)
alert.addAction(UIAlertAction(title: "Close", style: UIAlertActionStyle.Cancel, handler:handleCancel))
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler:{ (UIAlertAction)in
println("User click Ok button")
println(self.textField.text)
}))
self.presentViewController(alert, animated: true, completion: {
println("completion block")
})
Can you see also my answer here
I see you're already using the new UIAlertController
-- good idea, since there's little use in old API if you're usung Swift anyway. But alert.textFieldAtIndex:
won't work for that; it's for UIAlertView
only.
Luckily, UIAlertController has a method for adding text fields.