3

我正在使用,UIAlertView但我收到以下错误消息:

* 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“textFieldIndex (0) 超出了文本字段数组的范围”

但是我的里面没有textFieldsUIAlertView我从来没有在某个地方访问过它们......

这是我的代码:

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

但是,当我将“ delegate:self”更改为“”时,delegate:nil它不会因错误而崩溃,但是我的委托函数当然不起作用……知道问题是怎么回事吗?

编辑:

第二个 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];

}

委托方式:

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

}
4

2 回答 2

7

这里设置不同tagUIAlertView并使用下面的代码......

你会得到错误,因为当你当时使用你 alertNewVersion UIAlertView的时候,它会在 0 索引处找到 textField,而这里它没有数组UITextFields并且应用程序在这里崩溃,所以使用下面的条件......

更新:

-(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);
       }
    }

}

使用此代码代替您上面的第一个代码...

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

对于消息,请参阅以下代码...

//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];
}
于 2013-04-26T09:59:13.500 回答
2

在以下方法中:

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

给您的警报视图名称并执行您想要的任何操作,如下所示:

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

    if(alertView == alertNewVersion)
    { 
    }   
    if(alertView == message)
    { 
    } 
}
于 2013-04-26T10:45:49.133 回答