iLearner
iLearner

Reputation: 1680

How to validate and restrict input to UITextField inside UIAlertView in iOS

I am using the UIAlertView to show the text field over a popup. But the problem I am facing is that I am not able to validate and restrict the entries in UITextField over a popup, i.e. I want to accept only 3 numeric values.

Here i am providing the block of code that I have implemented.

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

So can anyone help me out to resolve this issue? Thanks in advance.

Upvotes: 3

Views: 1694

Answers (2)

Suchi
Suchi

Reputation: 129

here I can see that you have not set textfield.delegate=self , please check it in your code

Upvotes: 2

Justin
Justin

Reputation: 20609

It is risky to manually add subviews to UIAlertView or any other view you do not control as its hierarchy is private and subject to change. If you're targeting iOS 5 or above, you can use one of the UIAlertView styles that supports text entry and validation such as UIAlertViewStylePlainTextInput. The documentation for UIAlertView provides a list of styles and this tutorial explains their use in more detail.

Upvotes: 1

Related Questions