使用SWIFT的ResetPassword-iOS项目中警报中的可选文本

Optional Text in Alert in ResetPassword - iOS Project using Swift(使用SWIFT的ResetPassword-iOS项目中警报中的可选文本)

本文介绍了使用SWIFT的ResetPassword-iOS项目中警报中的可选文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的iOS应用程序的ResetPassword按钮,使用Xcode和SWIFT:

//ResetPssword Button
@IBAction func ResetPassword(sender: AnyObject) {

    if validateEmail(EmailTextField.text!) ==  false {
        print("Enter a Valid Email Address")
        let VaildMessage = "Enter an Email Address"
        //Empty TextField Alert Message
        self.disaplayErrorMessage(VaildMessage)
   }

    //Reset
    else {

        ref.resetPasswordForUser(EmailTextField.text) { (ErrorType) -> Void in
            if ErrorType != nil {

                let error = ErrorType
                print("There was an error processing the request (error.description)")
                let errorMessage:String = "The Email You Entered is not Exist"
                //Error Alert Message
                self.disaplayErrorMessage(errorMessage)

            } else {

                print("Password Reset Sent Successfully")
                let successMessage = "Email Message was Sent to You at (self.EmailTextField.text)"

                //Success Alert Message
                self.disaplayErrorMessage(successMessage)
            }

    } //reset

    } //Big Else

 } //Button


//Display Alert Message With Confirmation
func disaplayErrorMessage(theMessage:String)
{
    //Display alert message with confirmation.
    let myAlert = UIAlertController(title: "Alert", message: theMessage, preferredStyle: UIAlertControllerStyle.Alert);

    let OkAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default) {
        action in
        self.dismissViewControllerAnimated(true, completion: nil);
    }
    myAlert.addAction(OkAction);
    self.presentViewController(myAlert, animated: true, completion: nil)
}


   //Validate Email Function
func validateEmail(candidate: String) -> Bool {
    let emailRegex = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,6}"
    return NSPredicate(format: "SELF MATCHES %@", emailRegex).evaluateWithObject(candidate)
}

如果密码重置成功发送,则会出现一个打印电子邮件地址的警报。但它会打印带有可选单词的电子邮件!

如果没有可选单词,我如何打印它?

推荐答案

您必须取消可选文本字段的换行

例如if let

if let text = self.EmailTextField.text {
    let successMessage = "Email Message was Sent to You at (text)"
    //Success Alert Message
    self.disaplayErrorMessage(successMessage)
} 

这篇关于使用SWIFT的ResetPassword-iOS项目中警报中的可选文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:使用SWIFT的ResetPassword-iOS项目中警报中的可选文本

基础教程推荐