I am quite new to SWIFT. I am trying the following checking method on my demo app. What is that I am doing wrong? Because the else if is never kicking in
class SignupClass: UIViewController {
@IBOutlet var nameActualUser: UITextField!
@IBOutlet var prefUsername: UITextField!
@IBOutlet var emailSignup: UITextField!
@IBOutlet var passSignup: UITextField!
@IBOutlet var phoneSignup: UITextField!
@IBAction func lawyerSignup(sender: AnyObject) {
hideRegistrationText()
}
@IBOutlet var regNumberSignup: UITextField!
@IBAction func validateEmail(sender: AnyObject) {
if emailSignup.text!.isEmpty {
let myAlert = UIAlertController(title: "Notification", message: "Enter email address", preferredStyle: UIAlertControllerStyle.Alert);
let okAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default){(ACTION) in
print("Ok Button Tapped");
}
myAlert.addAction(okAction)
self.presentViewController(myAlert, animated: true, completion: nil);
print("enter email address") //prompt ALert or toast
}
else if validate(emailSignup.text!)
{
let myAlert = UIAlertController(title: "Notification", message: "Invalid email addresses", preferredStyle: UIAlertControllerStyle.Alert);
let okAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default){(ACTION) in
print("Ok Button Tapped");
}
myAlert.addAction(okAction)
self.presentViewController(myAlert, animated: true, completion: nil);
print("Invalid email address") // prompt alert for invalid email
}
}
@IBOutlet var lawyerSwitch: UISwitch!
@IBAction func signupButton(sender: AnyObject) {
validateEmail(emailSignup.text!)
signupMethod()
}
func validate(emailAdd: String) -> Bool
{
let REGEX: String
REGEX = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,6}"
return NSPredicate(format: "SELF MATCHES %@", REGEX).evaluateWithObject(emailAdd)
}
func hideRegistrationText()
{
if lawyerSwitch.on
{
regNumberSignup.hidden = false
}
else
{
regNumberSignup.hidden = true
}
}
func signupMethod()
{
let user = PFUser()
user.username = prefUsername.text
user.password = passSignup.text
user.email = emailSignup.text
user["name"] = nameActualUser.text
user["phone"] = phoneSignup.text
user.signUpInBackgroundWithBlock{
(succeeded: Bool, error:NSError?) -> Void in
if let _ = error{
let myAlert = UIAlertController(title: "Notification", message: error!.localizedDescription, preferredStyle: UIAlertControllerStyle.Alert);
let okAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default){(ACTION) in
print("Ok Button Tapped");
}
myAlert.addAction(okAction)
self.presentViewController(myAlert, animated: true, completion: nil); }
else
{
let myAlert = UIAlertController(title: "Notification", message: "Succesfully Signed Up!", preferredStyle: UIAlertControllerStyle.Alert);
let okAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default){(ACTION) in
print("Ok Button Tapped");
}
myAlert.addAction(okAction)
self.presentViewController(myAlert, animated: true, completion: nil);
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
regNumberSignup.hidden = true
nameActualUser.text = "werewdfsd"
prefUsername.text = "edfvccer"
//emailSignup.text = ""
passSignup.text = "234324"
phoneSignup.text = "1231243"
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
} }
Aucun commentaire:
Enregistrer un commentaire