带文本字段的UIAlertView不显示字段

时间:2016-04-07 14:35:09

标签: uialertview textfield alertview

我正在尝试创建一个允许您编写注释的UIAlertView,但这些字段不会出现在AlertView上。这里是代码:

UIAlertView *prompt = [[UIAlertView alloc] initWithTitle:@"Your opinion"
                                                 message:@"Enter your comment" // IMPORTANT
                                                delegate:self
                                       cancelButtonTitle:@"Cancel"
                                       otherButtonTitles:@"Send", nil];



textField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 50.0, 260.0, 25.0)];
[textField setBackgroundColor:[UIColor blackColor]];
[textField setPlaceholder:@"Name"];
textField.keyboardAppearance = UIKeyboardAppearanceAlert;
textField.delegate = self;
[prompt addSubview:textField];

textField2 = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 85.0, 260.0, 50.0)];
[textField2 setBackgroundColor:[UIColor whiteColor]];
[textField2 setPlaceholder:@"Your comment"];
textField2.keyboardAppearance = UIKeyboardAppearanceAlert;
textField2.delegate = self;

lblCounter = [[UILabel alloc] initWithFrame:CGRectMake(100, 200, 120, 10)];
lblCounter.backgroundColor = [UIColor clearColor];
lblCounter.textColor = [UIColor whiteColor];
lblCounter.font = [UIFont systemFontOfSize:13.5];

lblCounter.text = @"0/100 characters";

[prompt addSubview:textField2];
[prompt addSubview:lblCounter];


[prompt show];

图像样本:

AlertView Sample

有人可以帮帮我吗?感谢

1 个答案:

答案 0 :(得分:0)

从iOS8开始,UIAlertView已被弃用。此外,它不打算以这种方式进行自定义:The view hierarchy for this class is private and must not be modified.(来自文档)。

我建议使用许多替换库中的一个,如SDCAlertView或滚动您自己的评论对话框。

如果您必须使用UIAlertView,请将alertViewStyle设置为UIAlertViewStyleLoginAndPasswordInput并更改密码文本字段以使用field.secureTextEntry = NO;

显示文字