In UIAlertView trying to view two textfield. Its showing error bounds of the array of text fields iOS7

StackOverflow https://stackoverflow.com/questions/23193317

Pregunta

Hi I'm trying to view two text fields in UIAlertView but its showing error. I have used the below code to view the text fields.

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Hello!"
                                                     message:@"Please enter your details:"
                                                    delegate:self
                                           cancelButtonTitle:@"Continue"
                                           otherButtonTitles:nil];
    alert.alertViewStyle = UIAlertViewStylePlainTextInput;

    UITextField * name = [alert textFieldAtIndex:0];
    name.keyboardType = UIKeyboardTypeAlphabet;
    name.placeholder = @"Enter your name";

    UITextField * phone = [alert textFieldAtIndex:1];
    phone.keyboardType = UIKeyboardTypeNumberPad;
    phone.placeholder = @"Enter your phone";

    [alert show];
    [alert release];
}

I have used the above code to view two textfileds but I'm getting like. 'textFieldIndex (1) is outside of the bounds of the array of text fields'

Please tell me where I'm doing wrong in the above code and please tell me show to resolve this issue.

¿Fue útil?

Solución

See UIAlertView.h file

/* Retrieve a text field at an index - raises NSRangeException when textFieldIndex is out-of-bounds. The field at index 0 will be the first text field (the single field or the login field), the field at index 1 will be the password field. */

- (UITextField *)textFieldAtIndex:(NSInteger)textFieldIndex NS_AVAILABLE_IOS(5_0);

Do like this:

alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;

UITextField * name = [alert textFieldAtIndex:0];
name.keyboardType = UIKeyboardTypeAlphabet;
name.placeholder = @"Enter your name";

UITextField * phone = [alert textFieldAtIndex:1];
phone.secureTextEntry = NO;
phone.keyboardType = UIKeyboardTypeNumberPad;
phone.placeholder = @"Enter your phone";
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top