代码之家  ›  专栏  ›  技术社区  ›  iOSdev mahendra

iOS 7:在文本字段上确实更改了第一响应程序,我的应用程序正在崩溃

  •  2
  • iOSdev mahendra  · 技术社区  · 12 年前

    我有一个可编辑的表视图单元格,当我从表中的第一个文本字段移动到最后一个文本字段时,它会崩溃。代码为。下面的代码用于文本字段委托

    -(void)textFieldDidBeginEditing:(UITextField *)sender
    {
       NSIndexPath *indexpath = [NSIndexPath indexPathForRow:sender.tag inSection:1];
        EditableTextFieldCell *cell = (EditableTextFieldCell *)[self.documentdetailTable cellForRowAtIndexPath:indexpath];
        self.actField = cell.textFieldOne;
        if([self.actField canBecomeFirstResponder]){
            [self.actField becomeFirstResponder];
        }
    }
     -(BOOL)textFieldShouldEndEditing:(UITextField *)textField
    {
       if (self.actField == textField) {
          [self.actField resignFirstResponder];
        }
        return YES;
    }
    -(void)textFieldDidEndEditing:(UITextField*)sender
    {
    if (self.quantityValue !=nil)
            {
                [self.quantityArray replaceObjectAtIndex:[sender tag] withObject:sender.text];
                [[self.documentItemsArray objectAtIndex:[sender tag]] setQUANTITY:[NSNumber numberWithDouble:[sender.text doubleValue]]];
                [self.documentdetailTable reloadData];
            }
    }
    - (BOOL)textFieldShouldClear:(UITextField *)textField {
    self.quantityValue=@"";
    return YES;
    }
    - (BOOL)textFieldShouldReturn:(UITextField *)textField
    {
        [textField resignFirstResponder];
        return YES;
    }
    -(void)textFieldDidChange:(UITextField *)theTextField
    {
        NSLog( @"text changed: %@", theTextField.text);
        self.quantityTextField = theTextField;
        self.actField = theTextField;
    
    }
    
    //add the textfield listeners
    [self.quantityTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingDidBegin];
    

    但它正在崩溃,我收到了这样的信息:

    **EditableTextFieldCell _didChangeToFirstResponder:]: message sent to deallocated instance 0xc3c6bf0**
    
    3 回复  |  直到 11 年前
        1
  •  8
  •   Jordi Corominas    12 年前

    我们遇到了同样的问题,这是因为我们使用“旧代码”创建/退出自定义UITableViewCells。。。

    我们要做的是在ViewDidLoad中添加这些行

    [self.myTableView registerClass:[ExpenseListCell class] forCellReuseIdentifier:@"ExpenseListCell"];
    [self.myTableView registerNib:[UINib nibWithNibName:@"ExpenseListCell" bundle:nil] forCellReuseIdentifier:@"ExpenseListCell"];
    

    然后“清理”函数cellForRowAtIndexPath,只使用出列函数:

    ExpenseListCell *cell = (ExpenseListCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    

    我怀疑,由于它们包括故事板,所以dequeueReusableCellWithIdentifier管理单元的创建,所以您不再需要我们在dequeue之后仍在使用的这些行:

    if(cell == nil)
    {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"ExpenseListCell" owner:cell options:nil] objectAtIndex:0];
    }
    

    注意:我们没有使用故事板

    改变这一点,解决了我们在iOS7上的问题。

        2
  •  4
  •   Natan R.    12 年前

    也许这个答案有些愚蠢,但却是正确的答案。 我已经检查了表视图单元格的rowatindexpath,并添加了一个标识符

    static NSString *EditableTextFieldCellIdentifier = @"EditableCell";
    
    // using custom cells to show textfield and multiple columns
    EditableTextFieldCell *cellText = [tableView dequeueReusableCellWithIdentifier:EditableTextFieldCellIdentifier];
    

    这解决了我的问题,也崩溃了。

        3
  •  0
  •   Brylle    12 年前

    有同样的问题。 我通过将标识符放置到表视图单元格的nib文件来修复它。 则在cellForRowAtIndexPath进行出列调用:

    static NSString *fsCellIdentifier = @"configurationCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:fsCellIdentifier];
    
    推荐文章