如何在iOS中的UIAlertView内验证和限制对UITextField的输入

时间:2012-01-10 06:17:42

标签: ios uitextfield uialertview

我正在使用UIAlertView在弹出窗口中显示文本字段。但我遇到的问题是我无法通过弹出窗口验证和限制UITextField中的条目,即我只想接受3个数值。

这里我提供了我已经实现的代码块。

popup = [[UIAlertView alloc] initWithTitle:@"Please enter 3 numeric values"
                                   message:@"\n\n"
                                  delegate:self
                         cancelButtonTitle:@"Cancel"
                         otherButtonTitles:@"Ok", nil];         

txtFld = [[UITextField alloc] initWithFrame:CGRectMake(12, 50, 260, 25)];
[txtFld setBackgroundColor:[UIColor whiteColor]];
[txtFld setKeyboardType:UIKeyboardTypeNumberPad];
[popup addSubview:txtFld];

[popup show];

[txtFld becomeFirstResponder];

那么有人可以帮我解决这个问题吗?提前谢谢。

2 个答案:

答案 0 :(得分:2)

在这里我可以看到你没有设置textfield.delegate = self,请在你的代码中查看

答案 1 :(得分:1)

将子视图手动添加到UIAlertView或您无法控制的任何其他视图是有风险的,因为其层次结构是私有的并且可能会发生变化。如果您的目标是iOS 5或更高版本,则可以使用支持文本输入和验证的UIAlertView样式之一,例如UIAlertViewStylePlainTextInputUIAlertView的文档提供了样式列表,this tutorial更详细地解释了它们的用法。

相关问题