Xcode:弹出窗口中有多个文本行

时间:2014-06-26 15:09:59

标签: objective-c xcode uitextfield uialertview

我尝试创建一个包含多个字段的文本框,但是我无法让第二个字段显示(事实上,当我键入第二个字段时,它会导致我的文本盒子不要一起出现。)

以下是我所拥有的:

-(IBAction)popupCheckIn {
//UIAlertView *alertCheckIn = [[UIAlertView alloc] initWithTitle:@"Check in" message:@"Please fill out the following to check in." delegate:self cancelButtonTitle:@"Check in." otherButtonTitles:@"Cancel.", nil];

//[alertCheckIn show];
UIAlertView * alert =[[UIAlertView alloc ] initWithTitle:@"Check in" message:@"Please fill out the following fields to check in." delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles: nil];


alert.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField * nameField = [alert textFieldAtIndex:0];
nameField.keyboardType = UIKeyboardTypeDefault;
nameField.placeholder = @"Your Name";

alert.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField * hostField = [alert textFieldAtIndex:1];
hostField.keyboardType = UIKeyboardTypeDefault;
hostField.placeholder = @"Host Name";

[alert addButtonWithTitle:@"Check in."];
[alert show];

当我运行此代码时,我收到一条错误,上面写着"线程1:信号SIGABRT"我的弹出不会出现;当我只有名字字段时,它工作正常。

我的第二个文字字段出了什么问题?谢谢!

3 个答案:

答案 0 :(得分:1)

我认为您的错误是因为UIAlertView类型不包含多个UITextField,而在尝试访问第二个时它会引发NSRangeException。这是根据文档。

https://developer.apple.com/library/ios/documentation/uikit/reference/UIAlertView_Class/UIAlertView/UIAlertView.html#//apple_ref/occ/instm/UIAlertView/textFieldAtIndex

答案 1 :(得分:1)

我尝试了你的代码,整个错误信息是:

2014-06-26 17:13:56.213 Testing1[2444:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'textFieldIndex (1) is outside of the bounds of the array of text fields'

问题是,您只有一个UITextFieldUIAlertViewStyle设置为UIAlertViewStylePlainTextInput。因此这部分代码([alert textFieldAtIndex:1]导致崩溃)。 重复行alert.alertViewStyle = UIAlertViewStylePlainTextInput;不会创建新行。

获得2 UITextField的唯一方法是使用UIAlertViewStyleLoginAndPasswordInput UIAlertViewStyle。 然后可以设置第二个(如第一个)就像这样:

[hostField setSecureTextEntry:FALSE];

但就个人而言,我认为我不推荐它。它可能在将来被阻止。

由于我们无法自定义现有的UIAlertView自iOS7(无法添加子视图),我建议你创建(或在CocoaControls / GitHub中找到)你自己的CustomAlertView。

答案 2 :(得分:0)

您希望使用警报样式UIAlertViewStyleLoginAndPasswordInput的警报。请参阅此答案:UIAlertView with Two TextFields and Two Buttons