2014-07-20 4 views
0

J'ai cherché sur le net mais n'ai pas trouvé de réponse. dans mon application j'ai une inscription d'utilisateur, donc je dois vérifier le champ de texte d'entrée avant d'accepter l'utilisateur.Plusieurs erreurs montrent dans le même UIAlertView

voici mon 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]; 
} 

mais si plus d'une erreur se produit à l'utilisateur obtenir beaucoup de pop ups. est-il possible d'afficher toutes les erreurs dans le même UIAlertview?

Répondre

2

Vous pouvez utiliser 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"]; 
    } 

} 
+0

super, merci! – OshriALM

1

Je voudrais juste concaténer des chaînes et leur montrer à la fin de la vérification d'erreur:

- (IBAction)continueRegister:(id)sender { 
NSMutableString * titleString= @""; 
NSMutableString * mesageString = @""; 


if (!termsOfUseChecked) 
    NSLog(@"Error"); 
    else 
     NSLog(@"Success"); 
if ([_regTelephoneNumber.text length] > 10) { 
[titleString appendString: @"an error title; "]; 
[messageString appendString: @"an error message; "] 
} 

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

} 

Vous pouvez également formater le messageString avec newLineCharacters en ajoutant « \ n "où vous voulez un saut de ligne.

Questions connexes