objective-c - 无法在 iOS7 上将 UITextField 添加到 UIAlertView ...在 iOS 6 中有效

标签 objective-c ios7 uitextfield uialertview uialertviewdelegate

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

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];

为 iOS6 运行的代码显示如下:

enter image description here

iOS7 中的相同代码显示了这一点(注意 UITextField 是如何丢失并且没有键盘):

enter image description here

最佳答案

在 iOS 7 中,您无法轻松更改 UIAlertView 的 View 层次结构。(您也不应该这样做;文档明确告诉您不要这样做。)前往开发者论坛查看有关它的长时间讨论。

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

关于objective-c - 无法在 iOS7 上将 UITextField 添加到 UIAlertView ...在 iOS 6 中有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18549519/

相关文章:

Objective-C 消息传递

objective-c - libclang 从游标中获取类名

iOS 7 文本工具包 : When has NSLayoutManager filled its last NSTextContainer?

ios - 在不更改支持字符串的情况下以大写形式呈现文本? (iOS7 和文本工具包)

iphone - 当我在 iOS 7 中弹出 ViewController 时需要再次显示 TabBar

ios - 滚动后 UITableViewCell 标签发生变化

ios - (ObjC -> Swift) 转换函数 "init"& "textureNamed"

iphone - 如何为多个值设置本地通知?

ios - 使用核心数据和 NSFetchedResultsControllers 预填充 UITextField

ios - UITextField attributedPlaceholder 颜色和不透明度在 swift 4