我想在其中创建一个包含两个uitextfield的警报视图。
method:
//show alertview for file input
- (IBAction)showAddFiles:(id)sender {
UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Enter File Details"
message:nil
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Add", nil];
UITextField *textFieldDescription = [message textFieldAtIndex:0];
textFieldDescription.placeholder = @"File Description : Ex. Acat Briefing";
UITextField *textFieldFileName = [message textFieldAtIndex:1];
textFieldFileName.placeholder = @"Exact File Name : Ex. acat.pdf";
[message show];
}
//make sure file description is long enoguh
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
NSString *inputText = [[alertView textFieldAtIndex:0] text];
if( [inputText length] <= 15 && [inputText length] >= 4)
{
return YES;
}
else
{
return NO;
}
}
//handle add button
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:@"Add"])
{
UITextField *fileDescription = [alertView textFieldAtIndex:0];
UITextField *fileName = [alertView textFieldAtIndex:1];
NSLog(@"Desc: %@\nName: %@", fileDescription.text, fileName.text);
}
}
错误:
*由于未捕获的异常'NSInvalidArgumentException'而终止应用,原因:'textFieldIndex(0)超出了文本字段数组的范围'
为什么我会收到此错误,如何在警报视图中创建两个uitextfield?
=========工作解决方案=========== 感谢下面的答案,只需要两个纯文本字段
//show alertview for file input
- (IBAction)showAddFiles:(id)sender {
UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Enter File Details"
message:nil
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Add", nil];
[message setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];
UITextField *fileDescription = [message textFieldAtIndex:0];
fileDescription.placeholder=@"Ex. acat.pdf";
[[message textFieldAtIndex:1] setSecureTextEntry:NO];
UITextField *fileName= [message textFieldAtIndex:1];
fileName.placeholder=@"Ex. Acat Briefing";
[message show];
}
答案 0 :(得分:24)
分配“消息”警报视图后。将其添加到您的代码中:
[message setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];
[[message textFieldAtIndex:1] setSecureTextEntry:NO];
这将使你的警报视图里面有两个文本字段。
答案 1 :(得分:4)
您收到的错误是因为UIAlertView
,'消息'中没有文字字段。实例方法“textFieldAtIndex”用于访问使用特定样式创建的UIAlertView
中的textField,例如UIAlertViewStylePlainTextInput
,UIAlertViewStyleSecureTextInput
或UIAlertViewStyleLoginAndPasswordInput
。这些样式在属性“alertViewStyle”上设置。例如:
[message setAlertViewStyle:UIAlertViewStylePlainTextInput];
设置此属性后,您可以使用“textFieldAtIndex”,但不幸的是,这些样式似乎都不适合您的需要。
我以前做的是创建默认样式的UIAlertView(就像你已经完成的那样),并将UITextFields作为子视图添加到UIAlertView。
例如:
//Create the alert then add any labels and text fields as subviews.
//You can pad out an alertView by adding newline characters in the message. This will
// give the alertView more space to draw the text fields.
UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Two Text Field Alert"
message:@"\n\n\n\n\n"
delegate:self
cancelButtonTitle:@"CanceL"
otherButtonTitles:@"OK", nil];
UITextField *textField1 = [[UITextField alloc] initWithFrame:CGRectMake(16,83,252,25)];
textField1.borderStyle = UITextBorderStyleRoundedRect;
textField1.keyboardAppearance = UIKeyboardAppearanceAlert;
textField1.delegate = self;
[message addSubview:textField1];
UITextField *textField2 = [[UITextField alloc] initWithFrame:CGRectMake(16,112,252,25)];
textField2.placeholder = @"Password";
textField2.borderStyle = UITextBorderStyleRoundedRect;
textField2.keyboardAppearance = UIKeyboardAppearanceAlert;
textField2.delegate = self;
[message addSubview:textField2];
[message show];
[message release];
[textField2 release];
[textField1 release];
与alertView样式相比,以这种方式登录更加冗长和混乱,但您可以根据需要调整此值,以便将任意数量的子视图添加到警报视图。
编辑简化示例。
答案 2 :(得分:3)
您收到该错误,因为UIAlertView
不包含任何文本字段。由于当您尝试拨打[alertView textFieldAtIndex:0]
时警报视图的文本字段集合为空,因此您最终会遇到NSInvalidArgumentException
并崩溃。
答案 3 :(得分:1)
我们被允许在alertview中只创建两个文本字段。这里:
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Change Password"
message:@""
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"OK", nil];
alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
UITextField * alertTextField1 = [alert textFieldAtIndex:0];
alertTextField1.keyboardType = UIKeyboardTypeDefault;
alertTextField1.placeholder = @"Type Current password";
[[alert textFieldAtIndex:0] setSecureTextEntry:YES];
UITextField * alertTextField2 = [alert textFieldAtIndex:1];
alertTextField2.keyboardType = UIKeyboardTypeDefault;
alertTextField2.placeholder = @"Type New Password";
[alert show];
答案 4 :(得分:0)