iOS 7: on textfield did change the firstresponder my application is crashing

ⅰ亾dé卋堺 提交于 2019-12-01 08:08:05

问题


I have editable tableview cell,and when i move from first textfield to last textfield in the table it is crashing.Code is.The below code is for textfield delegate

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

But it is crashing and I am getting message like:

**EditableTextFieldCell _didChangeToFirstResponder:]: message sent to deallocated instance 0xc3c6bf0**

回答1:


We had the same issue and it was because we where using "old code" to create/dequeue the custom UITableViewCells...

What we had to do was to add this lines in the ViewDidLoad

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

and then "clean" the function cellForRowAtIndexPath to just use the dequeue function:

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

I suspect that, since they included storyboards, the dequeueReusableCellWithIdentifier manages the creation of the cells, so you don't need anymore this lines that we where still using after the dequeue:

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

NOTA: We are not using storyboards

Changing this, solved our problem on iOS7.




回答2:


May be this answer somewhat is stupid but the correct answer. I have checked for tableview cellforrowatindexpath and added an identifier

static NSString *EditableTextFieldCellIdentifier = @"EditableCell";

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

And this fixed my problem and also crash.




回答3:


Had the same issue. I fixed it by placing identifier to the table view cell's nib file. then a dequeue call at cellForRowAtIndexPath:

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


来源:https://stackoverflow.com/questions/19087867/ios-7-on-textfield-did-change-the-firstresponder-my-application-is-crashing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!