我在使用 UIAlertViews 时遇到了一个非常奇怪的错误。把我的头发弄得乱七八糟,请帮帮我!
当我按下“创建!”时发生错误在 alert1 警报中,checkUsernameExists 返回 true,然后我按“K”。在 alert3 中。
代码
-(void) newProfile:(id)sender {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"New Profile"
message:nil
delegate:self
cancelButtonTitle:@"Create!"
otherButtonTitles:nil];
alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
UITextField *username = [alert textFieldAtIndex:0];
username.keyboardType = UIKeyboardTypeDefault;
username.placeholder = @"Desired Username";
UITextField *password = [alert textFieldAtIndex:1];
password.keyboardType = UIKeyboardTypeDefault;
password.placeholder = @"Desired Password";
[alert1 show];
}
- (void)alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString *title = [alert title];
UITextField *username = [alert textFieldAtIndex:0];
UITextField *passwrod = [alert textFieldAtIndex:1];
if ([title isEqualToString:@"New Profile"]) {
if ([self checkUsernameExists:username.text] == false) {
// Create new account
}
else {
[self errorPop:@"New Profile"];
}
}
}
-(void) errorPop:(NSString*)who {
if ([who isEqualToString:@"New Profile"]) {
UIAlertView *alert3 = [[UIAlertView alloc] initWithTitle:@"OH NOOOOO"
message:@"Username is already taken please try another one."
delegate:self
cancelButtonTitle:@"K."
otherButtonTitles:nil];
[alert3 show];
}
}
日志
2014-07-30 19:57:49.594 App[6532:a0b] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'textFieldIndex (0) is outside of the bounds of the array of text fields'
最佳答案
第一个警报 View 不包含文本字段,因此当您请求 -clickedButtonAtIndex:
中的第一个或第二个文本字段时,您会遇到越界异常。
您应该区分您正在接收回调的警报 View 。尝试在创建时将警报 View 存储在属性中,然后在尝试访问文本字段之前在回调中检查它们的身份。
关于ios - 多个 UIAlertViews 的错误/错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25036525/