Reputation: 1137
In the below code I have a button which will prompt user to enter my admin passphrase. If I click Ok button it should be able to set message to inputTextField and display the message
var inputTextField: UITextField?
@IBAction func logOutButton(sender: AnyObject) {
let actionController = UIAlertController(title: "Log out", message: "Are you sure you want to log out?", preferredStyle: UIAlertControllerStyle.Alert)
actionController.addTextFieldWithConfigurationHandler { (textField) in
textField.placeholder = "Admin passphrase"
textField.secureTextEntry = true
self.inputTextField = textField
}
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default){UIAlertAction in
if self.inputTextField != nil{
print(self.inputTextField)
}else{
print("Nothing")
}
self.dismissViewControllerAnimated(true, completion: nil)
}
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Default){UIAlertAction in
print("Test: Log out action cancelled")
}
actionController.addAction(okAction)
actionController.addAction(cancelAction)
self.presentViewController(actionController, animated: true, completion: nil)
}
This is what I get from the console after entering "admin" and click ok
Console message: Optional(<_UIAlertControllerTextField: 0x7fc90bc58d00; frame = (4 4; 229 16); text = 'admin'; clipsToBounds = YES; opaque = NO; gestureRecognizers = ; layer = >)
Everything works fine until I change my the following to
var inputTextField: String?
self.inputTextField = textField.text
and result: Optional("")
I'm certain that something must have sent to inputTextField but I can't get it out, any idea?
Upvotes: 3
Views: 870
Reputation: 1010
You can also access the textField of UIAlertController by this way :
let alertView = UIAlertController(title: "title", message: "Type the msg", preferredStyle: UIAlertControllerStyle.Alert)
alertView.addTextFieldWithConfigurationHandler { (textField) -> Void in
textField.placeholder = "Admin passphrase"
textField.secureTextEntry = true
}
let alertAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) { (alertAction) -> Void in
let val = (alertView.textFields![0] as UITextField).text! as String
if val == "" {
print("nothing")
} else {
print(val)
}
}
alertView.addAction(alertAction)
presentViewController(alertView, animated: true, completion: nil)
Upvotes: 4
Reputation: 598
You need to get text from the prombt alertview. Use following code.
let actionController = UIAlertController(title: "Log out", message: "Are you sure you want to log out?", preferredStyle: UIAlertControllerStyle.Alert)
actionController.addTextFieldWithConfigurationHandler { (textField) in
textField.placeholder = "Admin passphrase"
textField.secureTextEntry = true
//self.inputTextField = textField // remove
}
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default){UIAlertAction in
if self.inputTextField != nil{
let textField:UITextField = (actionController.textFields?[0])!;
self.inputTextField.text = textField.text;
NSLog("%@",textField.text!)
}else{
print("Nothing")
}
self.dismissViewControllerAnimated(true, completion: nil)
}
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Default){UIAlertAction in
print("Test: Log out action cancelled")
}
actionController.addAction(okAction)
actionController.addAction(cancelAction)
self.presentViewController(actionController, animated: true, completion: nil)
Upvotes: 0
Reputation: 848
Just use this inside the OK button function.
// myText will be the first textField in alert controller, store its text into ans
let myText = actionController.textFields![0]
let ans = myText.text
if ans != ""{
print(ans)
}else{
print("Nothing")
}
This will get the first textField inside your alertController, and then you just store the it in any variable and do you condition.
Upvotes: 1