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];
}
After you allocated the “message” alert view. Add this to your code:
This will make your alert view two text field inside.