0

I use SCLAlertView Framework as my Alert View. https://github.com/vikmeup/SCLAlertView-Swift

I create an alertview with textfield. Here is the code.

let emailSubmit: UITextField = UITextField() let passwordSubmit: UITextField = UITextField() var email: String? var password: String? //This function call when loginbutton tapped func showLogin(){ let alertView = SCLAlertView() let emailSubmit = alertView.addTextField("Enter your Email") let passwordSubmit = alertView.addTextField("Password") email = emailSubmit.text password = passwordSubmit.text passwordSubmit.secureTextEntry = true alertView.addButton("Confirm", target:self, selector:#selector(ViewController.submitLogin)) alertView.showTitle( "Login", subTitle: "", duration: nil, completeText: "Cancel", style: .Info) } 

It required me to add a function for the button. The function name is submitLogin. It use to send the login detail(textfield.text) to the back-end server. However, it only return nil value after i click the submit button It is the submit login button function

func submitLogin(){ let email = emailSubmit.text! let password = passwordSubmit.text! print("\(email),\(password)") } 

However, It display nil when I click the submit button Can anyone point out what's wrong with the code?

2 Answers 2

2

I think there are two different emailSubmit and passwordSubmit variables present in your code. The one outside the function showLogin is the one you are using in submitLogin method but those fields are initialized without any value on lines 1 and 2. There is an another instance initialized and added to the SCLAlertView inside the showLogin method and displayed but its value is not used otherwise.

You should probably use blocks like how it is suggested in the sample code

Sign up to request clarification or add additional context in comments.

1 Comment

oh yes! I should use block!!
1

The Code should be like this:

var email: String? var password: String? //This function call when loginbutton tapped @IBAction func showLogin(){ let alertView = SCLAlertView() let emailSubmit = alertView.addTextField("Enter your Email") let passwordSubmit = alertView.addTextField("Password") passwordSubmit.secureTextEntry = true alertView.addButton("Confirm") { self.email = emailSubmit.text self.password = passwordSubmit.text self.submitLogin() } alertView.showTitle( "Login", subTitle: "", duration: nil, completeText: "Cancel", style: .Info) } func submitLogin(){ print("\(email),\(password)") } 

Explanation:

In your code the line password = passwordSubmit.text is setting value of var password to nil before user presses the confirm button, same with var email.

And there is not need to Define email and password textfields, since alertview will create textfields within itself.

1 Comment

omg... I should not create the sector to call the function. It will be more complicated. Thanks mate ! Well Explanation !

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.