我正在实现一个简单的对话框,要求输入名称。这是我的代码:
UIAlertView* dialog = [[UIAlertView alloc] init];
[dialog setDelegate:self];
[dialog setTitle:@"Enter Name"];
[dialog setMessage:@" "];
[dialog addButtonWithTitle:@"Cancel"];
[dialog addButtonWithTitle:@"OK"];
UITextField *nameField = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)];
[nameField setBackgroundColor:[UIColor whiteColor]];
[dialog addSubview:nameField];
CGAffineTransform moveUp = CGAffineTransformMakeTranslation(0.0, 20.0);
[dialog setTransform: moveUp];
[dialog show];
[dialog release];
[nameField release];
它过去工作正常,但在iOS 7中,文本字段并没有显示出来。有人可以帮忙吗?
答案 0 :(得分:2)
这在iOS7中不再有效 试试这个
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:nil message:NSLocalizedString(@"Enter a phone number",nil) delegate:self cancelButtonTitle:NSLocalizedString(@"Cancel",nil) otherButtonTitles:NSLocalizedString(@"OK",nil), nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert setTag:1];
[[alert textFieldAtIndex:0] setDelegate:self];
[[alert textFieldAtIndex:0] resignFirstResponder];
[[alert textFieldAtIndex:0] setPlaceholder:NSLocalizedString(@"Enter Number here", nil)];
[[alert textFieldAtIndex:0] setKeyboardType:UIKeyboardTypeNumberPad];
[[alert textFieldAtIndex:0] becomeFirstResponder];
[alert show];
[alert release];
您可以根据需要更改键盘类型。
祝你好运:)答案 1 :(得分:0)
尝试使用这样的...并且不需要将文本字段添加为子视图。
UIAlertView* dialog = [[UIAlertView alloc] init];
[dialog setDelegate:self];
[dialog setTitle:@"Enter Name"];
[dialog setMessage:@" "];
[dialog addButtonWithTitle:@"Cancel"];
[dialog addButtonWithTitle:@"OK"];
dialog.alertViewStyle = UIAlertViewStylePlainTextInput;
CGAffineTransform moveUp = CGAffineTransformMakeTranslation(0.0, 20.0);
[dialog setTransform: moveUp];
[dialog show];
[dialog release];
[nameField release];