具有多个文本输入的 UIAlertView?

UIAlertView with multiple text inputs?

    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Add new work order (TEST)" message:@"Please fill out all fields" delegate:self cancelButtonTitle:@"Add" otherButtonTitles: nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField *alertTextFieldName = [alert textFieldAtIndex:0];
alertTextFieldName.placeholder = @"Work order name";
alertTextFieldName.keyboardType = UIKeyboardTypeDefault;
UITextField *alertTextFieldProjectNo = [alert textFieldAtIndex:1];
alertTextFieldProjectNo.placeholder = @"ProjectNo";
alertTextFieldProjectNo.keyboardType = UIKeyboardTypeDefault;
UITextField *alertTextFieldSubName = [alert textFieldAtIndex:2];
alertTextFieldSubName.placeholder = @"Sub account name";
alertTextFieldSubName.keyboardType = UIKeyboardTypeDefault;
[alert show];

我正在尝试这样做,但出现错误:NSRangeException',原因:'*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]'

我不能向 aletView 添加多个文本字段吗? -textFieldAtIndex: 让我知道这是可能的。我错了吗?

我觉得这个thread会很有用。
如果您只需要两个字段,那么继续 UIAlertViewStyleLoginAndPasswordInput
否则,为您自己的子视图提供您的逻辑所需的一切。

P.S。 textFieldAtIndex 方法可用于接收已经存在的文本字段,但您正在尝试接收尚不存在的字段。

使用此代码在 UIAlertView

中添加多个文本字段
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 250, 100)];

UITextField *textField1 = [[UITextField alloc] initWithFrame:CGRectMake(10,0,252,25)];
textField1.borderStyle = UITextBorderStyleRoundedRect;
textField1.placeholder = @"Username";
textField1.keyboardAppearance = UIKeyboardAppearanceAlert;
textField1.delegate = self;
[v addSubview:textField1];

UITextField *textField2 = [[UITextField alloc] initWithFrame:CGRectMake(10,30,252,25)];
textField2.placeholder = @"Password";
textField2.borderStyle = UITextBorderStyleRoundedRect;
textField2.keyboardAppearance = UIKeyboardAppearanceAlert;
textField2.delegate = self;
[v addSubview:textField2];


UITextField *textField3 = [[UITextField alloc] initWithFrame:CGRectMake(10,60,252,25)];
textField3.placeholder = @"Address";
textField3.borderStyle = UITextBorderStyleRoundedRect;
textField3.keyboardAppearance = UIKeyboardAppearanceAlert;
textField3.delegate = self;
[v addSubview:textField3];


UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"TEST" message:@"" delegate:nil cancelButtonTitle:@"NO" otherButtonTitles:@"YES", nil];
[av setValue:v  forKey:@"accessoryView"];
[av show];

希望这段代码对你有用。