6

我想创建一个包含两个 uitextfields 的警报视图。

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) 超出了文本字段数组的范围”

为什么会出现此错误,如何在警报视图中创建两个 uitextfields?

=========工作解决方案===========感谢以下答案,当您只需要两个纯文本字段时

//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];
}
4

5 回答 5

24

在您分配“消息”警报视图之后。将此添加到您的代码中:

[message setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];
[[message textFieldAtIndex:1] setSecureTextEntry:NO];

这将使您的警报视图里面有两个文本字段。

于 2012-11-05T15:32:02.577 回答
4

UIAlertView您收到的错误是因为您的“消息”中没有文本字段。存在实例方法“textFieldAtIndex”以访问UIAlertView 使用特定样式(如 、 或 )创建的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 样式相比,以这种方式登录更加冗长和混乱,但您可以根据需要调整此方式以将任意数量的子视图添加到警报视图。

编辑以简化示例。

于 2012-11-05T15:46:12.080 回答
3

您收到该错误是因为UIAlertView不包含任何文本字段。由于在您尝试调用时警报视图的文本字段集合是空的[alertView textFieldAtIndex:0],因此您最终会NSInvalidArgumentException遇到崩溃。

于 2012-11-05T15:36:14.863 回答
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];
于 2014-12-06T11:54:38.733 回答
0

这是您问题的解决方案.. http://www.alterplay.com/ios-dev-tips/2009/12/username-and-password-uitextfields-in-uialertview-prompt.html

于 2012-11-05T15:30:42.343 回答