UIAlertView error outside of the bounds of the array of a text field but not using textfields
ios, iphone, objective-c, uialertview, xcode
Solution
Here set different `tag` to every `UIAlertView` and use bellow code...
You get error because here when you use your `alertNewVersion` `UIAlertView` at that time its go to found the textField at 0 index and here it have not Array of `UITextFields` and application crashed here so use bellow condition...
UPDATE:
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (alertView.tag == 1) {
if (buttonIndex == 1) {
/// do nothing here
}
}
else if (alertView.tag == 2){
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:@"Ok"])
{
UITextField *txtName = [alertView textFieldAtIndex:0];
UITextField *txtClass = [alertView textFieldAtIndex:1];
NSLog(@"Name: %@\nClass: %@", txtName.text, txtClass.text);
}
}
}
use this code instead fo your first above code...
UIAlertView *alt = [[UIAlertView alloc]initWithTitle:@"Update" message:@"There is a new version" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
alt.tag = 1;
[alt show];
[alt release];
And For Message see this bellow code...
//show alertview for message
- (IBAction)startPushed:(id)sender {
UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"test."
message:nil
delegate:self
cancelButtonTitle:nil
otherButtonTitles:@"Ok", nil];
[message setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];
UITextField *text1 = [message textFieldAtIndex:0];
text1.placeholder=@"xxx";
[[message textFieldAtIndex:1] setSecureTextEntry:NO];
UITextField *text2= [message textFieldAtIndex:1];
text2.placeholder=@"xxx";
message.tag = 2;
[message show];
}
Problem
I'm using the `UIAlertView` but I get the following error message: * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'textFieldIndex (0) is outside of the bounds of the array of text fields' However there are not `textFields` in my `UIAlertView` and I never access them somewhere... This is my code: ``` alertNewVersion = [[UIAlertView alloc] initWithTitle: @"Update" message: @"There is a new version" delegate: self cancelButtonTitle:@"OK" otherButtonTitles:@"Cancel",nil]; [alertNewVersion setAlertViewStyle:UIAlertViewStyleDefault]; alertNewVersion.tag = 1; [alertNewVersion show]; ``` However when I change the "`delegate:self`" to "`delegate:nil`" it doesn't crash with the error but ofcourse my delegate functions don't work then... Any idea how the problem could be? EDIT: Second UIAlertView: ``` - (IBAction)startPushed:(id)sender { message = [[UIAlertView alloc] initWithTitle:@"test." message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil]; [message setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput]; [[message textFieldAtIndex:1] setSecureTextEntry:NO]; [[message textFieldAtIndex:0] setPlaceholder:@"xx"]; [[message textFieldAtIndex:1] setPlaceholder:@"xxx"]; message.tag = 2; [message show]; } ``` Delegate method: ``` -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { if(alertView.tag == 1){ // alertNewVersion if (buttonIndex == 0) { //button "OK" clicked NSLog(@"test"); } } if(alertView.tag == 2){ // Message if (buttonIndex == 0) { //button "OK" clicked NSString *Name = [[message textFieldAtIndex:0] text]; [[NSUserDefaults standardUserDefaults] setObject:Name forKey:@"xx"]; NSString *Class = [[message textFieldAtIndex:1] text]; [[NSUserDefaults standardUserDefaults] setObject:Class forKey:@"xxx"]; } } } ```