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

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运行的代码显示:

在此处输入图像描述

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

在此处输入图像描述

推荐答案

在iOS 7中,您无法轻松更改UIAlertView的视图层次 struct .(你也不应该这样做;文件明确告诉你不要这样做.)前往开发者论坛,观看关于它的长时间讨论.

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

Objective-c相关问答推荐

日期更改时的 UIDatePicker

iOS:警告try 呈现其视图不在窗口层次 struct 中的 ViewController

防止其他程序员调用 -init 的最佳方法

使用 [NSBundle mainBundle] pathForResource: ofType:inDirectory: 访问文件

在 viewDidAppear 之前无法正确设置框架

混合 Objective-C 和 C++

使用 UIView animateWithDuration 进行动画处理时无法 UIButton

如何隐藏/显示导航栏中的右键

警告的含义在演示过程中!

iOS - 从应用程序获取 CPU 使用率

如何在 iPhone 中将 NSData 转换为字节数组?

dyld:找不到符号:try 运行 iOS 应用程序时的 _NSURLAuthenticationMethodClientCertificate

Objective-C:块 vs. Select 器 vs. 协议

NSManagedObjectContext:异常断点在 save: 方法处停止,但没有日志(log)/崩溃/错误

Objective-C 的 NSMutableArray 是线程安全的吗?

iPhone:在 UITextField 上禁用 Auto-Cap/autocorrect 问题

如何在 UIAlertController 中添加 textField?

在我滚动之前,数据不会加载到 UITableView 中

NSString 中子字符串的出现次数?

Xcode 的不完整实现警告