if nameTextField.text!.isEmpty not working? - swift

Im trying to check if a textFieldis empty, and if it is a popup should show:
func missingText (title: String, message: String)
{
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: { (action) in
alert.dismiss(animated: true, completion: nil)
}))
self.present(alert, animated: true, completion: nil)
}
#IBAction func registerButton(_ sender: UIButton) {
//Display errormsg for missing entry
if nameTextField.text!.isEmpty
{
missingText(title: "Error", message: "Missing name")
}
}
Nothing is happening when the textfield is empty. I (think) I have the exact same code in another app, and that's working fine.. What am I missing?
The nameTextFieldis hooked up.

I had a similar problem once, It turned out that instead of using the placeholder for description for the textfield, I used the Text. Both are located in Attributes inspector for the textField. So, if u use the Textfor description, the textField will never be empty.

I guess textfield text is nil. so that it may not be calling. try with below code
#IBAction func registerButton(_ sender: UIButton) {
guard let text = textField.text, text != "" else{
missingText(title: "Error", message: "Missing name")
return
}
// do stuff with text,
}

u can try this one this might will help you . nametextfield.text , !text.empty

Related

Else statement triggers twice in Swift

I've been trying to register a user to Firebase and while doing that I'm checking if there is an error. If error occurs, then my code block pops up a UIalertcontroller and prints "error" once So far it works fine.
When there is no error then it should do a segue and print "segue done". But While it has no error the code triggers segue twice and prints "segue done" twice. Why this happens?
#IBAction func registerButton(_ sender: Any) {
firestoreManager.registerUser(mail: mailField.text!, fullName: fullNameField.text!, username: usernameField.text!, password: passwordField.text!, instruments: instrumentArray, registerUserError: { [self] (canRegister) in
if canRegister == false {
print("registeruserfail")
let alert = UIAlertController(title: "Error", message: firestoreManager.errorString, preferredStyle: .alert)
self.present(alert, animated: false) {
alert.message = firestoreManager.errorString
alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil))
print("error")
}
}
else { // this statement triggers twice
performSegue(withIdentifier: "toMainPage", sender: self)
print("segue done")
return
}
})
}

AWS Cognito doesn't call confirmSignUp when confirming user identity in Swift with Xcode

I finally managed to get the SignUp to work, but when trying to confirm the signup, I'm reaching a problem. Here is my code:
var user: AWSCognitoIdentityUser?
#IBAction func submitButton(_ sender: Any) {
guard let confirmationCodeValue = self.codeTextField.text, !confirmationCodeValue.isEmpty else {
let confirmationAlert = UIAlertController(title: alertTitle, message: alertMessage, preferredStyle: .alert)
confirmationAlert.addAction(UIAlertAction(title: "好", style: .default, handler: {action in
print("Try again!")
}))
self.present(confirmationAlert, animated: true, completion: nil)
return
}
self.user?.confirmSignUp(self.codeTextField.text!, forceAliasCreation: true).continue({[weak self] (task: AWSTask) -> Any? in
guard let strongSelf = self else { return nil }
DispatchQueue.main.async(execute: {
print("At least this is working...")
if let error = task.error {
let confirmationFailAlert = UIAlertController(title: (error as NSError).userInfo["__type"] as? String,
message: (error as NSError).userInfo["__type"] as? String,
preferredStyle: .alert)
confirmationFailAlert.addAction(UIAlertAction(title: "好",
style: .default,
handler: {action in
}))
self?.present(confirmationFailAlert, animated: true, completion: nil)
} else {
let confirmationSuccessAlert = UIAlertController(title: self?.alertTitleConfirmationComplete,
message:self?.alertMessageConfirmationComplete,
preferredStyle: .alert)
confirmationSuccessAlert.addAction(UIAlertAction(title: "好",
style: .default,
handler: {action in
self?.dismiss(animated: true, completion: nil)
}))
self?.present(confirmationSuccessAlert, animated: true, completion: nil)
}
})
return nil
})
}
The first part of this code works fine. If I type nothing in the space, I get an alertView telling me so. However, if I type anything in the space, nothing happens. The print statement "At least this is working..." never gets called. I've been staring at this code for a couple hours now trying to figure out what's wrong, and I feel like it's probably something simple, but as of now, I could use some help!
Thanks in advance!
I assume the code block above is not the full source, but be sure the optional, user, is "unencapsulated" and equal to an actual instance of AWSCognitoIdentityUser.
If it isn't, which I am assuming it is not, confirmSignUp won't know the username, sub, or have any information on the user it is "confirming".
I would recommend logging user and be sure that username is in fact a value within user.
I believe you set it equal to that instance type in the response to your AWSCognitoIdentityUserPool class signUp:password:userAttributes:validationData: call.
Check those values that are returned in AWSCognitoIdentityUserPoolSignUpResponse.

How To Make an Alert Only Appear Once

I'm trying to figure out is how to create a pop up window that only appears once when you start the app and then won't appear again unless you close the app and reboot it. However, if you view the code below, you will realize that the alert will pop up every time the ViewController appears. For example, if you go to the settings tab and then return to the main ViewController, then the alert will pop up.
override func viewDidAppear(animated: Bool) {
let alertController = UIAlertController(title: "Disclaimer", message: "WARNING: Please ride carefully!", preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "Accept", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alertController, animated: true, completion: nil)
}
Just create a global variable that is a Bool. If the app is opened it starts at false. Then once the disclaimer is seen it sets the variable to true. Then present the View Controller based on the value of the variable.
var disclaimerHasBeenDisplayed = false
class ViewController {
override func viewDidAppear(animated: Bool) {
if disclaimerHasBeenDisplayed == false {
disclaimerHasBeenDisplayed = true
let alertController = UIAlertController(title: "Disclaimer", message: "WARNING: Wakeboarding is fun, however it can be highly dangerous.
Wake Dice is not liable for any injuries obtained while wakeboarding. Please ride carefully!", preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "Accept", style: UIAlertActionStyle.Default,handler: nil))
self.presentViewController(alertController, animated: true, completion: nil)
}
}
}

Getting text value from alert box

I'm having problems getting a text fields value from a UIAlertController. I am getting the value, but It seems to be executing my code in the wrong order. In the function below, I expect the function call to return a string, but the value is not set at the time the function returns.
#IBAction func btnSaveSession(sender: AnyObject) {
//Prompt user to enter session name
var sessionName: String = ""
sessionName = promptUserToEnterSessionName("Save Session", message: "Please enter the name of your custom session below.");
print("session Name: " + sessionName)
//Save session to firebase.
//redirect to create session controller.
}
func promptUserToEnterSessionName(title: String, message: String) -> String{
var sessionName: String = ""
//1. Create the alert controller.
let alert = UIAlertController(title: title, message: message, preferredStyle: .Alert)
//2. Add the text field. You can configure it however you need.
alert.addTextFieldWithConfigurationHandler({ (textField) -> Void in
textField.text = "Enter session name."
})
//3. Grab the value from the text field, and print it when the user clicks OK.
alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: { (action) -> Void in
let textField = alert.textFields![0] as UITextField
print("Text field: \(textField.text)")
sessionName = textField.text!
}))
// 4. Present the alert.
self.presentViewController(alert, animated: true, completion: nil)
return sessionName
}
If anyone is having problems understanding my question, please comment and I will do my best to explain.
your ok action handler(closure) will call when you click on ok button. so instead of assign value to local variable sessionName , make a sessionName variable in class.
class YourClass {
var sessionName: String?
...
#IBAction func btnSaveSession(sender: AnyObject) {
//Prompt user to enter session name
promptUserToEnterSessionName("Save Session", message: "Please enter the name of your custom session below.");
}
func promptUserToEnterSessionName(title: String, message: String) {
//1. Create the alert controller.
let alert = UIAlertController(title: title, message: message, preferredStyle: .Alert)
//2. Add the text field. You can configure it however you need.
alert.addTextFieldWithConfigurationHandler({ (textField) -> Void in
textField.text = "Enter session name."
})
//3. Grab the value from the text field, and print it when the user clicks OK.
alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: { (action) -> Void in
let textField = alert.textFields![0] as UITextField
print("Text field: \(textField.text)")
self.sessionName = textField.text!
self.testDemo()
}))
// 4. Present the alert.
self.presentViewController(alert, animated: true, completion: nil)
}
fun TestDemo() {
print("session Name: " + self.sessionName)
//Save session to firebase.
//redirect to create session controller.
}
}
You can use a Callback to get the value, like this:
func AlertPrompt(vista:UIViewController, titulo:String , mensaje:String, aceptar:String, completion: #escaping (_ resultado: String)->()) {
let miAlerta = UIAlertController(title: titulo, message: mensaje, preferredStyle: UIAlertControllerStyle.alert)
miAlerta.addTextField(configurationHandler: {
(textField) -> Void in
})
let okBoton = UIAlertAction(title: aceptar, style: UIAlertActionStyle.default, handler: {
(action) -> Void in
let result = miAlerta.textFields![0] as UITextField
// You return the value here
completion(result.text! as String)
})
miAlerta.addAction(okBoton)
let cancelBoton = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: nil)
miAlerta.addAction(cancelBoton)
vista.present(miAlerta, animated: true, completion: nil)
}
And call the function like this:
AlertPrompt(vista: self, titulo: "Title", mensaje: "some message", aceptar: "Acept"){(resultado)->() in
print(resultado)
}
That works perfect for me, hope this help
This code:
let textField = alert.textFields![0] as UITextField
print("Text field: \(textField.text)")
sessionName = textField.text!
will be called only at the moment the button corresponding to the defined action is clicked. So at the moment of creation the UIAlertController it's not called.
Read about blocks in Swift, it should be clearer then.

Why can't won't the MFMailComposerViewController be dismissed?

Whenever I press "Cancel" then "Delete Draft", the mail composer won't be dismissed. The error I'm getting is "Thread 1: EXC_BAD_ACCESS (code=1, address=0x40363380)"
In my TableViewController I have:
#IBAction func mailButton(sender: AnyObject) {
let emailComposer = EmailComposer()
if email != "" {
print(email)
if emailComposer.canSendMail() {
emailComposer.setRecipient(email)
let configuredMailComposeViewController = emailComposer.configuredMailComposeViewController()
presentViewController(configuredMailComposeViewController, animated: true, completion: nil)
}
} else {
let alertController = UIAlertController(title: "Sorry!", message: "No email found for this contact", preferredStyle: .Alert)
alertController.addAction(UIAlertAction(title: "OK", style: .Default, handler: { (action) -> Void in
//do nothing
}))
self.presentViewController(alertController, animated: true, completion:nil)
}
}
For those who don't know, EXC_BAD_ACCESS means its trying to access something in memory that is no longer there. I wrongfully created the EmailComposer() object after the button tap so it was going out of scope. So this:
let emailComposer = EmailComposer()
...should have been created here, for example:
class TableViewController: UITableViewController {
let emailComposer = EmailComposer()