iosobjective-cuialertviewuialertviewdelegate

Multiple errors show in the same UIAlertView


I searched the net but have not found an answer. in my app i have a user registration, so i have to check the input text field before i accept the user.

here is my code:

- (IBAction)continueRegister:(id)sender {
    if (!termsOfUseChecked)
        NSLog(@"Error");
        else
            NSLog(@"Success");
    if ([_regTelephoneNumber.text length] > 10) {
        [self initWithTitle:@"error" andMessage:@"bla bla" andCancelButton:@"ok"];
    }

    if ([_regUserEmail.text rangeOfString:@"@"].location == NSNotFound){
        [self initWithTitle:@"error" andMessage:@"bla bla" andCancelButton:@"ok"];
    }
    if (![_regPassword.text isEqualToString:_regConfirmPassword.text]) {
     [self initWithTitle:@"error" andMessage:@"bla bla" andCancelButton:@"ok"];
    }

}

-(void)initWithTitle:(NSString*)title andMessage:(NSString*)message andCancelButton:(NSString*)cancelButton;
{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:title message:message delegate:self cancelButtonTitle:cancelButton otherButtonTitles:nil, nil];
    [alert show];
} 

but if more than one error occurs the user get many pop ups. there is any way to show all the errors in the same UIAlertview?


Solution

  • You can use NSArray

    - (IBAction)continueRegister:(id)sender {
        NSMutableArray *errorArr = [@[] mutableCopy];
        if (!termsOfUseChecked)
            NSLog(@"Error");
        else
            NSLog(@"Success");
        if ([_regTelephoneNumber.text length] > 10) {
            [errorArr addObject:@"Telephone Bla"];
        }
    
        if ([_regUserEmail.text rangeOfString:@"@"].location == NSNotFound){
            [errorArr addObject:@"UserEmail Bla"];
        }
        if (![_regPassword.text isEqualToString:_regConfirmPassword.text]) {
            [errorArr addObject:@"Password Bla"];
        }
    
        if([errorArr count]==0){
            [self initWithTitle:@"No Error" andMessage:@"Success" andCancelButton:@"ok"];
        }
        else if([errorArr count] == 1){
    
            [self initWithTitle:@"Error" andMessage:errorArr[0] andCancelButton:@"ok"];
        }
        else if([errorArr count] >1){
    
            [self initWithTitle:@"Multiple Error" andMessage[errorArr componentsJoinedByString:@","] andCancelButton:@"ok"];
        }
    
    }