Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

How can I return a boolean within this block?

I don't want the function to return false if the user enters invalid login credentials. I made a __block BOOL but that hasn't worked. I took a look at this post http://stackoverflow.com/questions/17642535/return-value-for-function-inside- and I don't understand how to implement a callback. How would that work in this scenario?

- (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;
}
}