如果用户输入无效的登录凭据,我不希望该函数返回false。我做了__block BOOL但是没有用。我看了一下这篇文章Return value for function inside a block 我不明白如何实现回调。在这种情况下,它会如何工作?
- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender {
__block BOOL ret=YES;
NSString *phoneNumberField = [self.phoneNumberField.text stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString *passwordField = [self.passwordField.text stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceAndNewlineCharacterSet]];
PFUser *newUser = [PFUser user];
newUser.username = phoneNumberField;
newUser.password = passwordField;
if ([identifier isEqualToString:@"Verify"]) {
if([newUser.username length] == 0 || [newUser.password length] == 0) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Oops! Sorry"
message:@"Please make sure you enter both a phone number and a password!" delegate: nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
return NO;
}
else
{
//PROBLEM IS BELOW
[newUser signUpInBackgroundWithBlock:^(BOOL succeeded, NSError * _Nullable error) {
if(!error)
{
ret = YES;
NSLog(@"Successful login");
}
else
{
ret = NO;
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Sorry!" message:@"That phone number is already in use" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alertView show];
}
}];
}
return ret;
} else {
return YES;
}
}