无法在iOS7上将UITextField添加到UIAlertView ...适用于iOS 6

iTr*_*out 64 objective-c uitextfield uialertview ios7 uialertviewdelegate

下面的代码适用于iOS6(以及之前的版本),但UITextField不会在iOS7中显示...有关如何在iOS7中的UIAlterView中显示UITextField的任何想法?

UIAlertView* dialog = [[UIAlertView alloc] init];
[dialog setDelegate:self];
[dialog setTitle:@"Enter ESC Score"];
[dialog setMessage:@" "];
[dialog addButtonWithTitle:@"Cancel"];
[dialog addButtonWithTitle:@"OK"];
dialog.tag = 5;

nameField = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)];
[nameField setKeyboardType:UIKeyboardTypeNumberPad];
[nameField becomeFirstResponder];
[nameField setBackgroundColor:[UIColor whiteColor]];
[dialog addSubview:nameField];
CGAffineTransform moveUp = CGAffineTransformMakeTranslation(0.0, 0.0);
[dialog setTransform: moveUp];
[dialog show];
[dialog release];

[nameField release];
Run Code Online (Sandbox Code Playgroud)

iOS6的代码运行显示:

在此输入图像描述

iOS7中的相同代码显示此内容(请注意UITextField如何丢失且没有键盘):

在此输入图像描述

Aar*_*ger 178

您无法轻松更改iOS 7中UIAlertView的视图层次结构.(您也不应该;文档特别告诉您不要这样做.)请访问开发人员论坛以查看有关它的长时间讨论.

在您的情况下,另一种选择是设置alert.alertViewStyle = UIAlertViewStylePlainTextInput; 这将为您添加一个文本字段.您可以使用UIAlertView委托回调访问它UITextField *textField = [alertView textFieldAtIndex:0];.

  • @loganathan`UIAlertViewStyleLoginAndPasswordInput`.您可以设置`[alertView textFieldAtIndex:1] .secureTextEntry = NO`如果你不想掩盖了第二个盒子.对于比这更复杂的事情,请自己观看. (9认同)
  • 这适用于单个文本字段.如果我想在uialertview中添加两个文本字段,那么解决方案是什么? (2认同)

iTr*_*out 17

@Aaron Brager有正确的解决方案.另外,在他建议默认数字键盘后,我添加了一行.

UIAlertView* dialog = [[UIAlertView alloc] init];
[dialog setDelegate:self];
[dialog setTitle:@"Enter ESC Score"];
[dialog setMessage:@" "];
[dialog addButtonWithTitle:@"Cancel"];
[dialog addButtonWithTitle:@"OK"];
dialog.tag = 5;

dialog.alertViewStyle = UIAlertViewStylePlainTextInput;
[dialog textFieldAtIndex:0].keyboardType = UIKeyboardTypeNumberPad;

CGAffineTransform moveUp = CGAffineTransformMakeTranslation(0.0, 0.0);
[dialog setTransform: moveUp];
[dialog show];
[dialog release];
Run Code Online (Sandbox Code Playgroud)


Man*_*ngh 6

UIAlertView *alertView = [[UIAlertView alloc]
                                  initWithTitle:@"Credit Card Number"
                                  message:@"Please enter your credit card number:"
                                  delegate:self
                                  cancelButtonTitle:@"Cancel"
                                  otherButtonTitles:@"Ok", nil];
        [alertView setAlertViewStyle:UIAlertViewStylePlainTextInput];
        /* Display a numerical keypad for this text field */
        UITextField *textField = [alertView textFieldAtIndex:0];
        textField.keyboardType = UIKeyboardTypeNumberPad;

[alertView show];
Run Code Online (Sandbox Code Playgroud)