我的代码以前工作正常,但由于某种原因今天运行它,它执行了不同的任务。我基本上试图在用户使用短用户名,电子邮件或密码时显示警告,否则将发出注册完成的警报,然后转到下一个视图。但是现在,只要我点击提交,它就会转到忽略警报的下一个视图。这是代码:
@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&#34; signUpDone&#34;在句柄中告诉它只进入下一个完整视图。但由于某种原因,它忽略了所有if语句,只是在没有条件或成功警报的单一警报的情况下进入下一个视图。我得到一个控制台信息,上面写着&#34;警告:尝试显示其视图不在窗口层次结构中!&#34;
我还没有碰过这些代码,但奇怪的是,当我今天尝试运行它时会发生这种情况。可能是什么问题呢?
答案 0 :(得分:0)
原来,segue直接从按钮连接到视图而不是视图,这迫使按钮立即转到视图而忽略if语句!