UIAlertController不在窗口层次结构中

时间:2016-07-31 16:37:56

标签: ios swift uialertview uialertcontroller

首先请不要在此问题上标记重复我已经完成了对此主题的研究,不仅最近的答案来自一年多以前,而且他们也一直在C#。我也不同于那些,因为我试图从我认为是孩子视角的孩子中呈现我的UIView。但我对此并不是百分之百确定。所以,这是我的代码转储在建议之后的样子。

import UIKit
import Firebase
class LoginViewController: UIViewController, UITextFieldDelegate{

@IBOutlet weak var usernameTxt: UITextField!
@IBOutlet weak var emailTxt: UITextField!
@IBOutlet weak var passwordTxt: UITextField!
@IBOutlet weak var confirmPassTxt: UITextField!
override func viewDidLoad() {
    super.viewDidLoad()

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
@IBAction func nextScreen(sender: UIButton) {


    if(emailTxt.text == "" || passwordTxt.text == "" || confirmPassTxt.text == "" || usernameTxt.text == ""){
        let alertController = UIAlertController(title: "Wait!", message: "You didn't fill out the required fields, please do so and try again. ", preferredStyle: .Alert)
        let defaultAction = UIKit.UIAlertAction(title: "OK", style: .Default, handler: nil)
        alertController.addAction(defaultAction)
        self.presentViewController(alertController, animated: true, completion: nil)
    }else{
        if(validateEmail(emailTxt.text!)){
            emailCheck(emailTxt.text!){isValid in if isValid{self.secondRound({ (goodMail, goodPassL, goodPassTxt, nameGood) in
                if (goodMail && goodPassL && goodPassTxt && !nameGood){
                    print("good")
                }else{
                   self.showAlert("Else", description: "Got it?")                    }

                })}else{let alertController=UIAlertController(title: "Whoops!", message: "That email address has already been taken, please try another one", preferredStyle: .Alert)
                let defaultAction = UIKit.UIAlertAction(title: "OK", style: .Default, handler: nil)
                alertController.addAction(defaultAction)
                alertController.parentViewController
                self.presentViewController(alertController, animated: true, completion: nil)
}}
              }else{
            let alertController = UIAlertController(title: "Whoops!", message: "That doesnt appear to be a valid email address, please check your information and try again!", preferredStyle: .Alert)
            let defaultAction = UIKit.UIAlertAction(title: "OK", style: .Default, handler: nil)
            alertController.addAction(defaultAction)
            alertController.parentViewController
            presentViewController(alertController, animated: true, completion: nil)


        }

    }





}
func showAlert(title: String, description: String){
    let alertController: UIAlertController = UIAlertController(title: title, message: description, preferredStyle: .Alert)
    let defaultAction = UIKit.UIAlertAction(title: "OK", style: .Default, handler: nil)
    alertController.addAction(defaultAction)
        UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(alertController, animated: true, completion: nil)


    self.presentViewController(alertController, animated: true, completion: nil)

}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    let DestinationVC : Login2VC = segue.destinationViewController as! Login2VC
    DestinationVC.prepareEmail = emailTxt.text!
    DestinationVC.preparePass = passwordTxt.text!

}

func validateEmail(canidate: String) -> Bool {
    let emailRegex = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,6}"
    return NSPredicate(format: "SELF MATCHES %@", emailRegex).evaluateWithObject(canidate)
}
func nameFilter(input : String)-> Bool{
    var profanity : Bool = true
    let dataRef = FIRDatabase.database().reference()
    dataRef.child("Profanity").observeSingleEventOfType(.Value) { (snap: FIRDataSnapshot) in
        if(snap.exists()){
            if(snap.value! as! NSArray).containsObject(input){
                print("our ears!")
                profanity = true
            }else{
            profanity = false
        }


    }


}
    return profanity
}
func emailCheck(input: String, callback: (isValid: Bool) -> Void) {
    FIRAuth.auth()?.signInWithEmail(input, password: " ") { (user, error) in
        var canRegister = false

        if error != nil {
            if (error?.code == 17009) {
                canRegister = false
            } else if(error?.code == 17011) {
                //email doesn't exist
                canRegister = true
            }
        }

        callback(isValid: canRegister)
    }
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
    textField.resignFirstResponder()
    return true
}
func secondRound(callback:(goodMail:Bool, goodPassL:Bool, goodPassTxt:Bool, nameGood:Bool)->Void){
    let availableEmail : Bool = true
    var passwordMatch : Bool = false
    var passwordLength : Bool = false
    var profanity : Bool = false
    if(passwordTxt.text!==confirmPassTxt.text!){passwordMatch=true}else{passwordMatch=false}
    if(passwordTxt.text!.characters.count>=6&&confirmPassTxt.text!.characters.count>=6){passwordLength=true}else{passwordLength=false}
    if(nameFilter(usernameTxt.text!)){profanity=true}else{profanity=false}
    callback(goodMail: availableEmail, goodPassL: passwordLength, goodPassTxt: passwordMatch, nameGood: profanity)


}
}

基本上,我要做的是:

  1. 检查输入的文本是否正确格式化为电子邮件
  2. 检查名称是否可用
  3. 检查用户名是否包含亵渎(从firebase拉为json
  4. 检查密码是否匹配
  5. 检查密码的长度是否至少为6个字符
  6. 每个false结果都有自己的UIAlertView,但每当我尝试添加这些视图时,它们都不会显示,并且应用会返回此错误。 注意,false事件完好无损地出现在第一个条件中。除此之外,没有任何事情发生。

    这是我一直在犯的错误,虽然看起来非常直截了当但我无法弄清楚如何修复它,也没有在网上找到有关如何完成此任务的最新信息。

    > Warning: Attempt to present <UIAlertController: 0x7fb513fb8bc0> on
    > <myApp.LoginViewController: 0x7fb513c6e0a0> whose view is not in the
    > window hierarchy!
    

    对此的合理答案是:

      

    嘿,你为什么不把窗口添加到层次结构中并完成?

    那么,对于那个问题,我有一个问题,如果窗口不是层次结构的一部分,那么为什么视图本身甚至会显示。此外,为什么第一组UIAlerts会显示,但一旦我进入嵌套if,它们就会停止?关于如何解决这个可怕错误的任何想法? 谢谢大家

0 个答案:

没有答案