Objective c Xcode:弹出窗口中有多个文本行
我试图创建一个包含多个字段的文本框,但在显示第二个字段时遇到了问题(事实上,当我输入第二个字段时,会导致文本框无法同时显示) 以下是我所拥有的:Objective c Xcode:弹出窗口中有多个文本行,objective-c,xcode,uitextfield,uialertview,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 cancel
-(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];
当我运行这段代码时,我得到一个错误,上面写着“thread1:SIGABRT信号”,我的弹出窗口根本不会出现;当我只有name字段时,它工作得很好
我的第二个文本字段有什么问题?谢谢 我认为出现错误是因为该类型的
UIAlertView
不包含多个UITextField,并且当尝试访问第二个UITextField时,会引发NSRangeException
。这是根据文件
:我尝试了你的代码,整个错误消息是:
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'
问题在于,您只有一个UITextField
,且UIAlertViewStyle
设置为UIAlertViewStylePlainTextInput
。因此,这部分代码([alert textfieldatinex:1]
导致崩溃)。
重复行alert.alertViewStyle=UIAlertViewStylePlainTextInput代码>不会创建新代码
获取2UITextField
s的唯一方法是使用uiAlertViewStyleLogin和PasswordInput
UIAlertViewStyle
。
然后,设置第二个(如第一个)的方法如下:
[hostField setSecureTextEntry:FALSE];
但就我个人而言,我认为我不推荐它。将来可能会被封锁
由于自iOS7(无法添加子视图)以来,我们无法定制现有的UIAlertView
,因此我建议您创建(或在CocoaControls/GitHub中找到)自己的CustomAlertView,如。您需要将alert与alert样式UIAlertViewStyleLoginAndPasswordInput一起使用。看看这个答案: