ios - 警报 Controller 不出现错误! Swift 项目

标签 ios xcode swift alert uialertcontroller

我正在尝试使用 swift 和 firebase 更改(编辑)iOS 应用程序中的用户电子邮件。 在更改电子邮件功能时,我添加了一些错误并针对每个错误发出警报。

当我模拟并测试错误时,例如,我尝试输入无效的电子邮件,错误正常工作! 但警报没有出现,它给了我这个错误,如下图所示。 enter image description here

代码如下:

    if EmailAddressTF.text=="" || PasswordTF.text=="" || BusinessNameTF.text==""
    {
        let alert = UIAlertController(title: "Oops!", message:"Make sure to fill in all required textfields", preferredStyle: .Alert)
        alert.addAction(UIAlertAction(title: "OK", style: .Default) { _ in })
        self.presentViewController(alert, animated: true){}
    }

    else {

        if RepasswordTF.text != PasswordTF.text
        {
            let alert = UIAlertController(title: "Oops!", message:"You Entered Different Passwords", preferredStyle: .Alert)
            alert.addAction(UIAlertAction(title: "OK", style: .Default) { _ in })
            self.presentViewController(alert, animated: true){}
        }

        else {

            let ref = Firebase(url: "https://businesswallet.firebaseio.com/")

            let mainpass = NSUserDefaults.standardUserDefaults().objectForKey("password") as? String
            let mainEmail = NSUserDefaults.standardUserDefaults().objectForKey("Email") as? String


            let isEqu = (mainEmail == self.EmailAddressTF.text)
            if(isEqu == true)
            {   }
            else
            {
                ref.changeEmailForUser(mainEmail, password: mainpass, toNewEmail: self.EmailAddressTF.text, withCompletionBlock: { error in

                    if error != nil {

                        if let errorCode = FAuthenticationError(rawValue: error.code) {

                            switch (errorCode) {
                            case .EmailTaken:
                                print("Email taken")
                                let alert = UIAlertController(title: "Oops!", message:"Sorry,Email taken", preferredStyle: .Alert)
                                alert.addAction(UIAlertAction(title: "OK", style: .Default) { _ in })
                                self.presentViewController(alert, animated:true){}

                            case .InvalidEmail:
                                print("invalid email")
                                let alert = UIAlertController(title: "Oops!", message:"invalid email", preferredStyle: .Alert)
                                alert.addAction(UIAlertAction(title: "OK", style: .Default) { _ in })
                               self.presentViewController(alert, animated:true){}


                            case .NetworkError:
                                print("Network Error")
                                let alert = UIAlertController(title: "Oops!", message:"Network error, check your connection", preferredStyle: .Alert)
                                alert.addAction(UIAlertAction(title: "OK", style: .Default) { _ in })
                                self.presentViewController(alert, animated: true){}

                            default:
                                print("Unknown Error")
                                let alert = UIAlertController(title: "Oops!", message:"Unknown Error", preferredStyle: .Alert)
                                alert.addAction(UIAlertAction(title: "OK", style: .Default) { _ in })
                                self.presentViewController(alert, animated: true){}
                            }
                        }





                    } else {

                        NSUserDefaults.standardUserDefaults().setValue(self.EmailAddressTF.text, forKey: "Email")
                        print("Email changed successfully")

                    }

                })

            } //Big Else

我该如何解决这个问题?

最佳答案

尝试在主线程中呈现警报

dispatch_async(dispatch_get_main_queue()) {
let alertController = UIAlertController(title: head, message: message, preferredStyle: .Alert)
let OKAction = UIAlertAction(title: "OK", style: .Default) { (action:UIAlertAction!) in
print("you have pressed OK button");
}
alertController.addAction(OKAction)

self.presentViewController(alertController, animated: false, completion:nil)
}

关于ios - 警报 Controller 不出现错误! Swift 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36654056/

相关文章:

iphone - 将类传递给函数

ios - atos 无法为架构 arm64 的文件 my.app.dSYM 加载符号

ios - 在 xcode 4.3 上运行测试

ios - 如何在 swift 中全局使用解析值

ios - 由于某些 base64 字符,我无法在 ios 中上传图片

ios - 如何将自定义图像添加到 Mapview Swift ios 中的注释?

ios - 滚动 UIScrollView 而不管触摸发生在何处

iOS 7 内容延伸到多个 UITableViewCells

ios - 在表格 View 中选择多个对象

ios - 当应用程序处于终止状态而无需用户交互时,如何清除本地通知。