多个错误显示在同一个 UIAlertView
Posted
技术标签:
【中文标题】多个错误显示在同一个 UIAlertView【英文标题】:Multiple errors show in the same UIAlertView 【发布时间】:2014-07-20 10:48:55 【问题描述】:我搜索了网络但没有找到答案。 在我的应用中,我有一个用户注册,所以我必须在接受用户之前检查输入文本字段。
这是我的代码:
- (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];
但如果发生多个错误,用户会收到很多弹出窗口。 有什么方法可以在同一个 UIAlertview 中显示所有错误?
【问题讨论】:
【参考方案1】:您可以使用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"];
【讨论】:
【参考方案2】:我只是连接字符串并在错误检查结束时显示它们:
- (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。
【讨论】:
以上是关于多个错误显示在同一个 UIAlertView的主要内容,如果未能解决你的问题,请参考以下文章