我有用于otp验证的textfield的alertview,但如果OTP不匹配,则另一个alertView将显示消息“OTP not matched”,然后点击ok按钮,然后再次出现以前的alertview(带OTP文本字段的alertview)进入OTP。
@IBAction func btnRegisterTapped(_ sender: UIButton) {
if !Validate(){
return
}
// get otp session
APIController.registerUser(Email: self.txtfldEmail.text!, Nonce: self.registerNonce) { (data1) in
print("data1 :=> \(data1)")
let alertController = UIAlertController(title: "BrainCal", message: "Please verify your OTP for login", preferredStyle: .alert)
let OkAction = UIAlertAction(title: "OK", style: .default, handler: {
alert -> Void in
APIController.registerUserWithOtp(Email: self.txtfldEmail.text!, Password: "", Nonce: self.registerNonce, Otp: (alertController.textFields?.first?.text!)!, Otp_Session: "\(data1["otp_session"]!)", SuccessHandler: { (response) in
print(response)
let dict = response as NSDictionary
if UserDefaults.standard.value(forKey: "LoginUserData") != nil{
UserDefaults.standard.removeObject(forKey: "LoginUserData")
}
UserDefaults.standard.set(NSKeyedArchiver.archivedData(withRootObject: dict), forKey: "LoginUserData")
self.performSegue(withIdentifier: "MainSegue", sender: self)
})
})
alertController.addTextField { (textField : UITextField!) -> Void in
textField.placeholder = "Enter Otp Here"
textField.keyboardType = .numberPad
NotificationCenter.default.addObserver(forName: NSNotification.Name.UITextFieldTextDidChange, object: textField, queue: OperationQueue.main) { (notification) in
OkAction.isEnabled = textField.text!.characters.count > 0
}
}
OkAction.isEnabled = false
alertController.addAction(OkAction)
self.present(alertController, animated: true, completion: nil)
}
}