我目前正在编写一个应用程序,该应用程序有一个以Parse为后台的登录和注册页面。我可以注册用户名和所有这些,但如果其中一个字段为空,我需要帮助编写通知用户的代码。我想显示错误的UIAlertView,并在这种情况下阻止视图切换(它切换,然后显示字段为空的通知)。我知道这可能很简单。只需要一点帮助。这是下面的代码。
-(IBAction)signUpUserPressed:(id)sender
{
PFUser *user = [PFUser user];
user.username = self.userRegisterTextField.text;
user.password = self.passwordRegisterTextField.text;
[user signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (!error) {
[self performSegueWithIdentifier:@"FinishSignup" sender:self];
} else {
[[[UIAlertView alloc] initWithTitle:@"Error" message:[error userInfo][@"error"] delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil] show];
}
}];
}
如果能提供帮助,我们将不胜感激。这也类似于你从应用程序中注销,它会询问你是否要注销——阻止视图切换和显示通知。
谢谢!
在调用signUpInBackgroundWithBlock之前:自己检查字段是否为空。
PFUser *user = [PFUser user];
user.username = self.userRegisterTextField.text;
user.password = self.passwordRegisterTextField.text;
if (user.username.length > 0 && user.password.length > 0) {
//sign up only if username/password given
[user signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (!error) {
[self performSegueWithIdentifier:@"FinishSignup" sender:self];
} else {
[[[UIAlertView alloc] initWithTitle:@"Error" message:[error userInfo][@"error"] delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil] show];
}
}];
}
}
else {
//show alert that username or password was left blank
}