Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create two textfields in an alert view, IOS

I want to create an alertview with two uitextfields inside of it.

method:
//show alertview for file input
- (IBAction)showAddFiles:(id)sender {
    UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Enter File Details"
                                                      message:nil
                                                     delegate:self
                                            cancelButtonTitle:@"Cancel"
                                            otherButtonTitles:@"Add", nil];



    UITextField *textFieldDescription = [message textFieldAtIndex:0];
    textFieldDescription.placeholder = @"File Description : Ex. Acat Briefing";
    UITextField *textFieldFileName = [message textFieldAtIndex:1];
    textFieldFileName.placeholder = @"Exact File Name : Ex. acat.pdf";


    [message show];
}


//make sure file description is long enoguh
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
    NSString *inputText = [[alertView textFieldAtIndex:0] text];

    if( [inputText length] <= 15 && [inputText length] >= 4)
    {
        return YES;
    }
    else
    {
        return NO;
    }
}

//handle add button
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
    if([title isEqualToString:@"Add"])
    {
        UITextField *fileDescription = [alertView textFieldAtIndex:0];
        UITextField *fileName = [alertView textFieldAtIndex:1];
        NSLog(@"Desc: %@\nName: %@", fileDescription.text, fileName.text);
    }
}

Error:

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'textFieldIndex (0) is outside of the bounds of the array of text fields'

Why I do get this error, how can I create two uitextfields in an alert view?

=========Working Solution =========== Thanks for the answer below works when you only need two plain textfields

//show alertview for file input
- (IBAction)showAddFiles:(id)sender {
    UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Enter File Details"
                                                      message:nil
                                                     delegate:self
                                            cancelButtonTitle:@"Cancel"
                                            otherButtonTitles:@"Add", nil];



    [message setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];
    UITextField *fileDescription = [message textFieldAtIndex:0];
    fileDescription.placeholder=@"Ex. acat.pdf";
    [[message textFieldAtIndex:1] setSecureTextEntry:NO];
    UITextField *fileName= [message textFieldAtIndex:1];
    fileName.placeholder=@"Ex. Acat Briefing";

    [message show];
}
like image 470
Mord Fustang Avatar asked Nov 05 '12 15:11

Mord Fustang


2 Answers

After you allocated the "message" alert view. Add this to your code:

[message setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];
[[message textFieldAtIndex:1] setSecureTextEntry:NO];

This will make your alert view two text field inside.

like image 153
Ulaş Sancak Avatar answered Oct 26 '22 13:10

Ulaş Sancak


The error you received occurs because there are no text fields in your UIAlertView, 'message'. The instance method "textFieldAtIndex" exists to access textFields in a UIAlertView that is created with a specific style, like UIAlertViewStylePlainTextInput, UIAlertViewStyleSecureTextInput, or UIAlertViewStyleLoginAndPasswordInput. These styles are set on the property "alertViewStyle". For example:

[message setAlertViewStyle:UIAlertViewStylePlainTextInput];

You may use "textFieldAtIndex" after setting this property, but unfortunately it looks as if none of these styles suit your needs.

What I've done before is to create a default styled UIAlertView (like you've already done), and add UITextFields as subviews to the UIAlertView.

For Example:

//Create the alert then add any labels and text fields as subviews.
//You can pad out an alertView by adding newline characters in the message.  This will
// give the alertView more space to draw the text fields.
UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Two Text Field Alert" 
                                                         message:@"\n\n\n\n\n" 
                                                        delegate:self 
                                               cancelButtonTitle:@"CanceL" 
                                               otherButtonTitles:@"OK", nil];

UITextField *textField1 = [[UITextField alloc] initWithFrame:CGRectMake(16,83,252,25)];
textField1.borderStyle = UITextBorderStyleRoundedRect;
textField1.keyboardAppearance = UIKeyboardAppearanceAlert;
textField1.delegate = self;
[message addSubview:textField1];

UITextField *textField2 = [[UITextField alloc] initWithFrame:CGRectMake(16,112,252,25)];
textField2.placeholder = @"Password";
textField2.borderStyle = UITextBorderStyleRoundedRect;
textField2.keyboardAppearance = UIKeyboardAppearanceAlert;
textField2.delegate = self;
[message addSubview:textField2];
[message show];

[message release];
[textField2 release];
[textField1 release];

It's a lot more verbose and messy to do a login this way as opposed to the alertView styles, but you can adapt this as you see fit to add any number of subviews to an alert view.

Edited to simplify example.

like image 44
Erracity Avatar answered Oct 26 '22 14:10

Erracity