ios - UIAlert 突然停止工作?

标签 ios swift uialertcontroller

我的代码曾经运行良好,但由于某种原因今天运行它,它执行了不同的任务。我基本上试图在用户的用户名、电子邮件或密码很短的情况下显示警报,否则将发出注册完成的警报,然后转到下一个 View 。但是现在,一旦我单击“提交”,它就会转到下一个 View ,忽略警报。这是代码:

@IBAction func signUp(_ sender: Any) {        
    // Validate the text fields
    if username!.count < 5 {
        let alert = UIAlertController(title: "Invalid", message: "Username must be greater than 5 characters", preferredStyle: UIAlertControllerStyle.alert)
        let action = UIAlertAction(title: "Okay", style: UIAlertActionStyle.default, handler: nil)
        alert.addAction(action)
        present(alert, animated: true, completion: nil)


    } else if password!.count < 8 {
        let alert = UIAlertController(title: "Invalid", message: "Password must be greater than 7 characters", preferredStyle: UIAlertControllerStyle.alert)
        let action = UIAlertAction(title: "Okay", style: UIAlertActionStyle.default, handler: nil)
        alert.addAction(action)
        present(alert, animated: true, completion: nil)

    } else if email!.count < 8 {
        let alert = UIAlertController(title: "Invalid", message: "Email must be greater than 7 characters", preferredStyle: UIAlertControllerStyle.alert)
        let action = UIAlertAction(title: "Okay", style: UIAlertActionStyle.default, handler: nil)
        alert.addAction(action)
        present(alert, animated: true, completion: nil)

    } else {
        let alert = UIAlertController(title: "Success", message: "Account has been created, an email will be sent shortly", preferredStyle: UIAlertControllerStyle.alert)
        let action = UIAlertAction(title: "Okay", style: UIAlertActionStyle.default, handler: {
            action in
            self.performSegue(withIdentifier: "signUpDone", sender: self)
        })
        alert.addAction(action)
        present(alert, animated: true, completion: nil)
        }
    }

正如你所看到的,我在句柄中有一个segue“signUpDone”,它告诉它只转到下一个完成 View 。但由于某种原因,它会忽略所有 if 语句,而只是进入下一个 View ,而没有任何条件警报或成功警报。我收到一条控制台信息,上面写着“警告:尝试呈现不在窗口层次结构中的 View !”

我还没有接触过代码,但奇怪的是,当我今天尝试运行它时,就会发生这种情况。可能是什么问题呢?

最佳答案

事实证明,segue 直接从按钮连接到 View ,而不是 View 到 View ,这迫使按钮立即转到 View ,而忽略 if 语句!

关于ios - UIAlert 突然停止工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47760091/

相关文章:

iPhone4如何查看电源线是否插好?

ios - 我的所有图像和sknodes在不同的iphone上都处于错误的位置

ios - 检测 collectionViewCell 内的设备旋转

swift - 在 Swift 3 中将字符串转换为整数数组?

iOS 9.0 - 从 UIAlertController 操作表中删除顶部栏

json - 如何在 Alamofire 请求 swift 3 上发出警报

ios - 以已插入收件人(我)的电子邮件形式发送文本?

ios - UITableViewCell 中的 UICollectionView 在 iOS 中无法正常工作

ios UIScrollView 有一个错误的默认 contentOffset - swift

ios - 在 IOS 中为框架创建 bundle ?