UIAlertView有多个文本输入?

时间:2015-03-28 11:48:43

标签: ios objective-c uialertview

    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:]:索引1超出界限[0 .. 0]'

我不应该能够在aletView中添加多个文本字段吗? -textFieldAtIndex:给我一个可能的概念。我错了吗?

2 个答案:

答案 0 :(得分:5)

使用此代码在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];

我希望这段代码对你有用。

答案 1 :(得分:0)

我认为这thread非常有用 如果您只需要两个字段,请继续使用 UIAlertViewStyleLoginAndPasswordInput
否则,请为您自己的子视图提供逻辑所需的所有内容。

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