【问题标题】:UIAlertView error outside of the bounds of the array of a text field but not using textfieldsUIAlertView 错误超出文本字段数组的范围但未使用文本字段
【发布时间】:2013-04-26 09:42:58
【问题描述】:

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

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

但是我的UIAlertView 中没有textFields,而且我从来没有在某个地方访问过它们...

这是我的代码:

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

}

【问题讨论】:

  • 嗯,很明显,问题出在你的委托函数上。你为什么不展示你在那里做什么?我打赌你正在使用 textFieldWithIndex
  • 请贴出应用崩溃的代码...
  • 我认为您在同一个课程中使用了多个警报视图,对吧??
  • 请贴出alertView委托方法的clickedButtonAtIndex方法...
  • 在抛出异常时添加断点。这应该会为您提供崩溃的确切位置。

标签: iphone ios objective-c xcode uialertview


【解决方案1】:

在这里为每个UIAlertView 设置不同的tag 并使用下面的代码...

您会收到错误,因为当您使用 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];
}

【讨论】:

  • @nonuma 您还在 .h 文件中添加了 UIAlertViewDelegate 吗?检查
  • 是的,我做到了,因为带有 textFields 输入的 UIAlertView 工作,它只是当我添加正常的 UIAlertView 时它给了我错误...
  • 看到我更新了我的答案,只是使用该代码而不是你 alloc alertNewVersion alertView.. 我确信它的问题并在 clickedButtonAtIndex 中崩溃了..
  • 我在我的项目中而不是我的项目中复制了您的答案,但正如我所说,即使我注释掉了另一个 alertview 并注释掉了整个 alertView 委托方法,它仍然是同样的错误
  • 它必须有效,伙计..尝试更改一些不同的标签,如设置 111 或 222 标签...我在 1 类中使用了许多 alertview,它的工作方式类似于上面...
【解决方案2】:

在下面的方法中:

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

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

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

    if(alertView == alertNewVersion)
    { 
    }   
    if(alertView == message)
    { 
    } 
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-24
    相关资源
    最近更新 更多