0

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?

4

2 回答 2

2

您可以使用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"];
    }

}
于 2014-07-20T10:58:27.400 回答
1

我只是连接字符串并在错误检查结束时显示它们:

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

}

您还可以通过在需要换行符的位置添加“\n”来使用 newLineCharacters 格式化 messageString。

于 2014-07-20T11:03:18.780 回答