ios - 重用 uitextfield 进行输入验证

标签 ios swift xcode authentication uitextfield

我正在创建登录流程,并且正在重新使用我的文本字段。我想知道我在这方面做错了什么,或者如何更改文本字段的验证。我创建了一个计数器,看看这是否有助于确定在该计数上使用的选项。当我进行电子邮件验证时,它无法正常工作。无论我输入什么,我都会收到警报。

final class SignupViewController: UIViewController, ViewPassesData, UITextFieldDelegate  {

@IBOutlet weak var firstNameTextField: UITextField!
@IBOutlet weak var lastNameTextField: UITextField!
@IBOutlet weak var backgroundViewImage: UIImageView!
@IBOutlet weak var firstLabel: UILabel!
@IBOutlet weak var secondLabel: UILabel!
@IBOutlet weak var button: UIButton!
@IBOutlet weak var secondLine: UIView!

enum SignUpStage {
    case name
    case email
    case password
    case phone
    case text
    case notifications
    case location
}

var stage: SignUpStage?
var dictionary: [String: String] = [:]




override func viewDidLoad() {
    super.viewDidLoad()
    setLayoutForStage()
}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    firstNameTextField.resignFirstResponder()
    lastNameTextField.resignFirstResponder()
}
func isValidEmail(email:String) -> Bool {
    let emailRegEx = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"
    let emailTest = NSPredicate(format:"SELF MATCHES %@", emailRegEx)
    let result = emailTest.evaluate(with: email)
    return result

}
func isPasswordValid(text: String) -> Bool {
    let passWordRegEx = "[A-Z0-9a-z]{0,9}"
    let passwordTest = NSPredicate(format:"SELF MATCHES %@", passWordRegEx)
    let result = passwordTest.evaluate(with: text)
    return result
}

@IBAction func validationTextField(_ sender: UITextField) {


}

这就是我的代码遇到问题的地方。 - 更新我已经在 @Julio Cesar Arregoitía Val 评论的帮助下解决了这个问题。

   @IBAction func actionButtonPressed(_ sender: UIButton) {
    guard let vc = storyboard?.instantiateViewController(withIdentifier: "Signup") as? SignupViewController, let stage = nextStage() else { return }
    vc.stage = stage
    setDictionaryValues()

    switch currentStage {
    case .name:
        if firstNameTextField.text == "" || lastNameTextField.text == "" {
           alertMessage()
        }
        else {
            vc.dictionary = dictionary
           self.navigationController?.pushViewController(vc, animated: true)
        }
    case .email:
        currentStage = .email
        if isValidEmail(email: "\(firstNameTextField.text!)") == true {
            vc.stage = stage
            vc.dictionary = dictionary
            self.navigationController?.pushViewController(vc, animated: true)
        }
        else {
            alertMessage()
        }
    case .password:
        currentStage = .password
        if firstNameTextField.text != "" && isPasswordValid(text: "\(firstNameTextField.text!)") == true {
            vc.stage = stage
            vc.dictionary = dictionary
            self.navigationController?.pushViewController(vc, animated: true)
        }
        else {
            alertMessage()
        }
    case .phone:
        currentStage = .phone
    case .text:
        currentStage = .text
    case .notifications:
        currentStage = .notifications
    case .location:
       currentStage = .location
    }
}


@IBAction func back(_ sender: Any) {
    navigationController?.popViewController(animated: true)
}

private func setLayoutForStage() {
    guard let stage = stage else { return }
    switch stage {
    case .name:
        firstLabel.text = "first name".uppercased()
        secondLabel.text = "last name".uppercased()
    case .email:
        firstLabel.text = "email".uppercased()
        secondLabel.isHidden = true
        lastNameTextField.isHidden = true
        secondLine.isHidden = true
    case .password:
         button.setNeedsDisplay()
        firstLabel.text = "password".uppercased()
        firstNameTextField.isSecureTextEntry = true
        secondLabel.isHidden = true
        lastNameTextField.isHidden = true
        secondLine.isHidden = true
    case .phone:
        firstLabel.text = "phone number".uppercased()
        firstNameTextField.keyboardType = .numberPad
        secondLabel.isHidden = true
        lastNameTextField.isHidden = true
        secondLine.isHidden = true
    case .text: break
    case .notifications: break
    case .location: break
    }
}
func resetTextFields() {

}

func setDictionaryValues() {
    guard let stage = stage else { return }
    switch stage {
    case .name:
        dictionary["first_name"] = firstNameTextField.text?.capitalizingFirstLetter()
        dictionary["last_name"] = lastNameTextField.text?.capitalizingFirstLetter()
    case .email:
        dictionary["email"] = firstNameTextField.text
        firstNameTextField.text = ""
    case .password:
        dictionary["password"] = firstNameTextField.text
        dictionary["password_confirmation"] = firstNameTextField.text
    case .phone:
        dictionary["phone_number"] = firstNameTextField.text
        dictionary["contact_preference"] = "EMAIL"
    case .text: break
    case .notifications: break
    case .location: break
    }
}

// TODO: make this all one system
private func nextStage() -> SignUpStage? {
    guard let stage = stage else { return .name }
    switch stage {
    case .name: return .email
    case .email: return .password
    case .password: return .phone
    case .phone:
        guard let nextVC = storyboard?.instantiateViewController(withIdentifier: "text") as? TextPhoneViewController, firstNameTextField.text?.count == 10 else {
            return nil
        }
        nextVC.dictionary = dictionary
        self.navigationController?.pushViewController(nextVC, animated: true)
        return nil
    case .text: return nil
    case .notifications: return nil
    case .location: return nil
    }
}

}

最佳答案

1-我认为你的主要问题 lem 是,在情况 0 中,i +=1 它仅放置在 else 中,并且当评估它时,您的 vc 由

self.navigationController?.pushViewController(vc, animated: true)

2-我认为您需要i来了解您处于哪个阶段。为此使用枚举变量:

var currentStage: SignUpStage = .name
@IBAction func actionButtonPressed(_ sender: UIButton) {

switch currentStage {
case .name:
    // Check name
    // ...
    // if it is ok
    fallthrough

case .email:
    currentStage = .email
    // Check .email:
    // ...
    // if it is ok
    fallthrough

case .password:
    currentStage = .password

    // All the others checks like this
}

fallthrough 让您仅在需要时才评估下一个条件,

3-您不需要输入:

lert.dismiss(animated: true, completion nil)

就这样做

lert.addAction( UIAlertAction(title: "OK", style: .default, handler: nil) )

你会得到相同的结果。

问候

关于ios - 重用 uitextfield 进行输入验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52022123/

相关文章:

ios - 在 Xcode 中包含 "-ObjC"的原因

iphone - 使用更新的配置文件重建 IPA 文件

ios - AVMediaTypeAudio 在 iOS 11 中不起作用

ios - 如何在单元格中设置 UILabel 相对于图像的位置

swift - 位置服务提示/授权未在 macos/swift 中激活

swift - 参数类型不符合预期类型 'WKScriptMessageHandler'

ios - 写入 MTLTexture 会导致 fatal error

ios - 将 PFUser 对象存储在 PFUser 键中会导致崩溃

swift - 在 Swift 中迭代一个自动闭包数组会导致编译错误

iphone - 如何在应用程序崩溃(或只是卡住)时显示错误消息?