Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIAlertView error outside of the bounds of the array of a text field but not using textfields

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

}
like image 407
nonuma Avatar asked Nov 24 '25 03:11

nonuma


2 Answers

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];
}
like image 129
Paras Joshi Avatar answered Nov 25 '25 15:11

Paras Joshi


In below method:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 

Give your alert view name & do what ever you want like below:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 

    if(alertView == alertNewVersion)
    { 
    }   
    if(alertView == message)
    { 
    } 
}
like image 24
Vishal Avatar answered Nov 25 '25 15:11

Vishal