所以我正在尝试创建一个注册页面,我遇到了一些麻烦。我希望用户在注册之前填写所有信息。在他们点击注册按钮后,如果他们没有填写所有字段,他们会得到许多不同的UIAlertView错误。当我点击注册时,它会将用户带到下一页然后显示UIAlertView,我不希望这种情况发生。我无法弄清楚发生了什么。如果UIAlertView没有填写用户名或电子邮件,我需要在点击按钮后显示UIAlertView,而不是转到下一页。我的代码中有一个performSegue方法,但它首先执行,而不是最后执行。有谁能够帮我?我还将包括我的故事板的屏幕截图。
user.signUpInBackgroundWithBlock {
(succeeded: Bool, error: NSError?) -> Void in
if let error = error {
let errorString = error.userInfo?["error"] as? NSString
if fullname.isEmpty || email.isEmpty || username.isEmpty || password.isEmpty {
var emptyFields:UIAlertView = UIAlertView(title: "Plese try again", message: "It looks like you forgot to fill out all the fields. Please make sure all the fields are filled out so we can create an account for you.", delegate: self, cancelButtonTitle: "Try again")
emptyFields.show()
} else if error.code == 203 {
var takenEmail:UIAlertView = UIAlertView(title: "Please try again", message: "It looks like that email has already been taken, please try again.", delegate: self, cancelButtonTitle: "Try again")
} else if error.code == 202 {
var usernameTaken:UIAlertView = UIAlertView(title: "Please try again", message: "It looks like that username is already in use, please try again.", delegate: self, cancelButtonTitle: "Try again")
}
}
}
答案 0 :(得分:2)
有几种方法可以做到这一点。我这样做的方式如下。
第1步。 好的,你想选择你的注册视图控制器。 第2步。 您将控制从黄色视图控制器图标单击并拖动到第二个视图控制器。在出现的菜单中,选择显示或呈现模态选项。
第3步。 选择刚刚创建的Segue并更改其标识符,在右侧面板中选择" signupSuccessful"。
第4步。 您可以像这样设置代码。
if usernameTF.text.isEmpty || passwordTF.text.isEmpty {
var emptyFields = UIAlertView()
emptyFields.title = "Plese try again"
emptyFields.message = "It looks like you forgot to fill out all the fields. Please make sure all the fields are filled out so we can create an account for you."
emptyFields.addButtonWithTitle("Try Again!")
emptyFields.show()
}else if usernameTF.text == "ERROR CODE 203" {
//do same thing with the alert view!
}else if usernameTF.text == "ERROR CODE 202" {
//Do another alert view here
//You can keep adding the "else ifs" for any other error codes
}
//Here is where it will send to the second view when everything above is false!
else {
//Here we present the second view.
self.performSegueWithIdentifier("signupSuccessful", sender: self)
}
你去吧。 如果您需要再次查看segue或上述任何代码,请下载该项目。
答案 1 :(得分:0)
在视图控制器中实施方法func shouldPerformSegueWithIdentifier(identifier: String?, sender: AnyObject?) -> Bool
,如果您不希望发生segue,请返回false
。