I'm trying to add an Ok button to a UI Alert but I cannot add it using alertController.addAction
What do I do in this case?
Thanks in advance!!
if error == nil {
let alert: UIAlertController = UIAlertController(title: "Account Created", message: "Please confirm your email", preferredStyle: .Alert)
let okButton = UIAlertAction(title: "Ok", style: .Default) { action -> Void in
self.performSegueWithIdentifier("toMain", sender: self)
alertController.addAction(okButton)
self.presentViewController(alert, animated: true, completion: nil)
}
} else {
println("\(error)")
}
alert instead of alertController
alert.addAction should be outside of your okbutton action.
Change your code with :
if error == nil {
let alert: UIAlertController = UIAlertController(title: "Account Created", message: "Please confirm your email", preferredStyle: .Alert)
let okButton = UIAlertAction(title: "Ok", style: .Default) { action -> Void in
self.performSegueWithIdentifier("toMain", sender: self)
}
alert.addAction(okButton)
self.presentViewController(alert, animated: true, completion: nil)
} else {
println("\(error)")
}
Related
I am trying to modulraise my code I am able to create extension for alertController without target
extension UIViewController {
func showAlert(title: String, message: String) {
let alertController = UIAlertController(title: title, message:
message, preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: {_ in
}))
self.present(alertController, animated: true, completion: nil)
}
}
but I am not sure how to create extension which have target and textfield(how to create and call that in viewcontroller)
my code for which i am trying to create extension
let alertController = UIAlertController(title: "enter name", message: nil, preferredStyle: .alert)
let addAction = UIAlertAction(title: "Ok", style: .default) {_ in
guard let name = alertController.textFields?.first?.text else { return }
let newTask = Task(name: name)
self.toDoStore?.add(newTask, at: 0)
let indexPath = IndexPath(row: 0, section: 0)
self.tableView.insertRows(at: [indexPath], with: .automatic)
}
addAction.isEnabled = false
let cancelAction = UIAlertAction(title: CANCEL_ACTION, style: .cancel, handler: nil)
alertController.addTextField { textField in
textField.placeholder = "enter name.."
textField.addTarget(self, action: #selector(self.handleTextChanged), for: .editingChanged)
}
alertController.addAction(addAction);
alertController.addAction(cancelAction);
present(alertController, animated: true, completion: nil)
You could try the below extension that I'm using made a few changes according to your requirements.
extension UIViewController {
func showAlert(_ title: String?,
message: String?,
actions: [String: UIAlertAction.Style] = [:],
handler: ((UIAlertAction) -> Void)? = nil,
textFields: [((UITextField) -> Void)] = []) {
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
if actions.isEmpty {
alert.addAction(UIAlertAction(title: "Okay", style: .default))
} else {
for action in actions {
alert.addAction(UIAlertAction(title: action.key, style: action.value, handler: handler))
}
}
for textField in textFields {
alert.addTextField(configurationHandler: textField)
}
present(alert, animated: true)
}
}
A bit of advise you can always check how the parameters that you pass are accepted by the system API and customize your own function in a similar way.
I have a action sheet and I open a alert with them with the following codelins:
func showActionSheet(postId: String) {
let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
let action = UIAlertAction(title: NSLocalizedString("Beitrag melden", comment: ""), style: .default, handler: { _ in
self.alertBeitrageMelden(postId: postId)
})
actionSheet.addAction(action)
actionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
self.present(actionSheet, animated: true, completion: nil)
}
I now want to store the input of the textfield inside the alert in Firebase:
func alertBeitrageMelden(postId: String){
// Create the action buttons for the alert.
let defaultAction = UIAlertAction(title: "Melden", style: .default) { (action) in
let ref = Database.database().reference().child("gemeldeteBeitraege").child(postId)
ref.setValue(["postId": postId, "reason": self.textFieldAlert])
}
let cancelAction = UIAlertAction(title: "Abbrechen", style: .cancel) { (action) in
// Respond to user selection of the action.
}
let alert = UIAlertController(title: "Beitrag melden", message: "Wieso möchtest du den Beitrag melden?", preferredStyle: .alert)
alert.addTextField { (textField) in
textField.placeholder = ""
if textField.text?.count ?? 0 > 0 {
self.textFieldAlert = textField.text!
}
}
alert.addAction(cancelAction)
alert.addAction(defaultAction)
self.present(alert, animated: true) {
}
}
I don't get any data of the textField.
Thanks in advance for your help!
You need
if let te = alert.textFields?.first?.text , te.count > 0 {
let ref = Database.database().reference().child("gemeldeteBeitraege").child(postId)
ref.setValue(["postId": postId, "reason":te])
}
As this self.textFieldAlert = textField.text! will store an empty initial value , and move this line
let alert = UIAlertController(title: "Beitrag melden", message: "Wieso möchtest du den Beitrag melden?", preferredStyle: .alert)
top of fucntion , so you can access it inside the alert action
func alertBeitrageMelden(postId: String) {
let alert = UIAlertController(title: "Beitrag melden", message: "Wieso möchtest du den Beitrag melden?", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Melden", style: .default) { (action) in
if let te = alert.textFields?.first?.text , te.count > 0 {
let ref = Database.database().reference().child("gemeldeteBeitraege").child(postId)
ref.setValue(["postId": postId, "reason":te])
}
})
alert.addAction(UIAlertAction(title: "Abbrechen", style: .cancel) { (action) in
// Respond to user selection of the action.
})
alert.addTextField { (textField) in
textField.placeholder = ""
}
self.present(alert, animated: true)
}
You can also do this by keeping a reference to the alert's textfield
var alertTexf:UITextField!
func alertBeitrageMelden(postId: String){
let alert = UIAlertController(title: "Beitrag melden", message: "Wieso möchtest du den Beitrag melden?", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Melden", style: .default) { (action) in
if let te = self.alertTexf.text , te.count > 0 {
let ref = Database.database().reference().child("gemeldeteBeitraege").child(postId)
ref.setValue(["postId": postId, "reason":te])
}
})
alert.addAction(UIAlertAction(title: "Abbrechen", style: .cancel) { (action) in
// Respond to user selection of the action.
})
alert.addTextField { (textField) in
textField.placeholder = ""
self.alertTexf = textField
}
self.present(alert, animated: true)
}
You can get your textField from your alertController using alert.textFields?.first.
Example:
func alertBeitrageMelden(postId: String){
// Create the action buttons for the alert.
let alert = UIAlertController(title: "Beitrag melden", message: "Wieso möchtest du den Beitrag melden?", preferredStyle: .alert)
alert.addTextField { (textField) in
textField.placeholder = ""
}
let defaultAction = UIAlertAction(title: "Melden", style: .default) { (action) in
if let textFieldAlert = alert.textFields?.first?.text, textFieldAlert.count > 0 {
let ref = Database.database().reference().child("gemeldeteBeitraege").child(postId)
ref.setValue(["postId": postId, "reason": textFieldAlert])
}
}
let cancelAction = UIAlertAction(title: "Abbrechen", style: .cancel) { (action) in
// Respond to user selection of the action.
}
alert.addAction(cancelAction)
alert.addAction(defaultAction)
self.present(alert, animated: true) {
}
}
alert.addTextField { textField in
}
This is alert's configuration handler where you can change properties of text field which will be added. But setting some text variable here doesn't make any sense because text of text field is empty in this moment.
Your goal should be get text of text field in moment when you need it, in this case in UIAlertAction's handler
So create alert before you declare its actions and add text field
let alert = UIAlertController(title: "Beitrag melden", message: "Wieso möchtest du den Beitrag melden?", preferredStyle: .alert)
alert.addTextField { textField in
textField.placeholder = ""
}
then in default action check if text of this text field isn't empty and if isn't save this text to Firebase
let defaultAction = UIAlertAction(title: "Melden", style: .default) { _ in
if let text = alert.textFields?.first?.text, !text.isEmpty {
let ref = Database.database().reference().child("gemeldeteBeitraege").child(postId)
ref.setValue(["postId": postId, "reason": text])
}
}
Hey guys so I'm doing registration form with Swift 4 and using Firebase.
I'm stuck on reseting the password. Whenever I click on the button "forget password" I should get a pop up window with textField to fill in my email address. But when I do click nothing happens. Posting code below if anyone have some ideas what can be wrong with it
#IBAction func forgotPasswordTapped(_ sender: Any) {
let forgotPasswordAlert = UIAlertController(title: "Forgot password?", message: "Enter email address", preferredStyle: .alert)
forgotPasswordAlert.addTextField { (textField) in
textField.placeholder = "Enter email address"
}
forgotPasswordAlert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
forgotPasswordAlert.addAction(UIAlertAction(title: "Reset Password", style: .default, handler: { (action) in
let resetEmail = forgotPasswordAlert.textFields?.first?.text
Auth.auth().sendPasswordReset(withEmail: resetEmail!, completion: { (error) in
if error != nil{
let resetFailedAlert = UIAlertController(title: "Reset Failed", message: "Error: \(String(describing: error?.localizedDescription))", preferredStyle: .alert)
resetFailedAlert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(resetFailedAlert, animated: true, completion: nil)
}else {
let resetEmailSentAlert = UIAlertController(title: "Reset email sent successfully", message: "Check your email", preferredStyle: .alert)
resetEmailSentAlert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(resetEmailSentAlert, animated: true, completion: nil)
}
})
}))
}
As a quick answer, you are simply forgetting to present your forgotPasswordAlert. The fix is simple:
#IBAction func forgotPasswordTapped(_ sender: Any) {
let forgotPasswordAlert = UIAlertController(title: "Forgot password?", message: "Enter email address", preferredStyle: .alert)
forgotPasswordAlert.addTextField { (textField) in
textField.placeholder = "Enter email address"
}
forgotPasswordAlert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
forgotPasswordAlert.addAction(UIAlertAction(title: "Reset Password", style: .default, handler: { (action) in
let resetEmail = forgotPasswordAlert.textFields?.first?.text
Auth.auth().sendPasswordReset(withEmail: resetEmail!, completion: { (error) in
if error != nil{
let resetFailedAlert = UIAlertController(title: "Reset Failed", message: "Error: \(String(describing: error?.localizedDescription))", preferredStyle: .alert)
resetFailedAlert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(resetFailedAlert, animated: true, completion: nil)
}else {
let resetEmailSentAlert = UIAlertController(title: "Reset email sent successfully", message: "Check your email", preferredStyle: .alert)
resetEmailSentAlert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(resetEmailSentAlert, animated: true, completion: nil)
}
})
}))
//PRESENT ALERT
self.present(forgotPasswordAlert, animated: true, completion: nil)
}
As a small aside, you'll want to make sure you are presenting your confirmation alerts on the main queue to avoid unexpected behavior. Perhaps the sendPasswordReset does this automatically, but I do not believe this to be the case. Additionally, the better way of getting the error description to present to the user is with optional binding (using if let).
Auth.auth().sendPasswordReset(withEmail: resetEmail!, completion: { (error) in
//Make sure you execute the following code on the main queue
DispatchQueue.main.async {
//Use "if let" to access the error, if it is non-nil
if let error = error {
let resetFailedAlert = UIAlertController(title: "Reset Failed", message: error.localizedDescription, preferredStyle: .alert)
resetFailedAlert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(resetFailedAlert, animated: true, completion: nil)
} else {
let resetEmailSentAlert = UIAlertController(title: "Reset email sent successfully", message: "Check your email", preferredStyle: .alert)
resetEmailSentAlert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(resetEmailSentAlert, animated: true, completion: nil)
}
}
})
For swift 4.2
import Firebase
// MARK: Firebase Forgotpassword
func callFIRPasswordReset(){
//show loader
Auth.auth().sendPasswordReset(withEmail: self.txtEmail.text!) { (error) in
DispatchQueue.main.async {
//hide loader
self.txtEmail.text = ""
if let error = error {
//show alert here
print(error.localizedDescription)
}
else {
//show alert here
print("We send you an email with instructions on how to reset your password.")
}
}
}
}
I wanna add message to phone call alert popup.
how to make it?
enter image description here
Yes, you can do it like that:
func phoneCall(to phoneNumber:String) {
if let callURL:URL = URL(string: "tel:\(phoneNumber)") {
let application:UIApplication = UIApplication.shared
if (application.canOpenURL(callURL)) {
let alert = UIAlertController(title: "Your Title", message: "Do you want call that number?", preferredStyle: .alert)
let callAction = UIAlertAction(title: "Call", style: .default, handler: { (action) in
application.openURL(callURL)
})
let noAction = UIAlertAction(title: "No", style: .cancel, handler: { (action) in
print("Canceled Call")
})
alert.addAction(callAction)
alert.addAction(noAction)
self.present(alert, animated: true, completion: nil)
}
}
}
In one of my viewController, I want to make an alert box appear that prompts the user to type this information.Then, I want the user to store this input using NSUserDefaults. How can I achieve this?
Thank you in advance!
Check this out:
let alertController = UIAlertController(title: "Email?", message: "Please input your email:", preferredStyle: .alert)
let confirmAction = UIAlertAction(title: "Confirm", style: .default) { (_) in
guard let textFields = alertController.textFields,
textFields.count > 0 else {
// Could not find textfield
return
}
let field = textFields[0]
// store your data
UserDefaults.standard.set(field.text, forKey: "userEmail")
UserDefaults.standard.synchronize()
}
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (_) in }
alertController.addTextField { (textField) in
textField.placeholder = "Email"
}
alertController.addAction(confirmAction)
alertController.addAction(cancelAction)
self.present(alertController, animated: true, completion: nil)
SWIFT 3
func presentAlert() {
let alertController = UIAlertController(title: "Email?", message: "Please input your email:", preferredStyle: .alert)
let confirmAction = UIAlertAction(title: "Confirm", style: .default) { (_) in
if let emailTextField = alertController.textFields?[0] {
// do your stuff with emailTextField
}
}
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (_) in }
alertController.addTextField { (textField) in
textField.placeholder = "Email"
}
alertController.addAction(confirmAction)
alertController.addAction(cancelAction)
present(alertController, animated: true, completion: nil)
}
In swift 3
let alertController = UIAlertController(title: "SecureStyle", message: "SecureStyle AlertView.", preferredStyle: UIAlertControllerStyle.Alert)
alertController.addTextFieldWithConfigurationHandler { (textField : UITextField) -> Void in
textField.secureTextEntry = true
textField.placeholder = "Password"
}
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) { (result : UIAlertAction) -> Void in
print("Cancel")
}
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) { (result : UIAlertAction) -> Void in
print(alertController.textFields?.first?.text)
}
alertController.addAction(cancelAction)
alertController.addAction(okAction)
self.presentViewController(alertController, animated: true, completion: nil)