2013-03-15 70 views
1

我設置了一個異常斷點並出現錯誤unrecognized selector sent to instance該實例是UITableViewCell本身。該應用程序使用CoreData,當UITextfield結束編輯時,我想將文本保存到NSManagedObject。Textfield沒有結束編輯崩潰應用程序

這裏是我的TableViewController內textFieldDidEndEditing方法:

- (void)textFieldDidEndEditing:(UITextField *)textField { 
    MCSwipeTableViewCell *cell = (MCSwipeTableViewCell *) textField.superview.superview; 
    TehdaItem *item = [self.fetchedResultsController objectAtIndexPath:[self.tableView indexPathForCell:cell]]; 


    item.itemTitle = cell.itemLabel.text; //The exception gets thrown on this line 
// itemLabel is a UITextField and itemTitle is a string attribute of TehdaItem the NSManagedObject 

    NSError *error; 
    [item.managedObjectContext save:&error]; 

    if (![self.fetchedResultsController performFetch:&error]) { 
     // Replace this implementation with code to handle the error appropriately. 
     // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. 
     NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
     abort(); 
    } 

} 

<UITextFieldDelegate>設置爲我TableViewController,我已經將其設置爲代表的的UITextField。無法真正弄清楚這裏的問題是什麼。

編輯:刪除最後superview調用MCSwipeTableViewCell *cell = (MCSwipeTableViewCell *) textField.superview.superview;已解決此問題。

+3

而你的問題的標題幾乎崩潰了我的大腦......;) – summea 2013-03-15 20:44:39

+0

請告訴我們完整的錯誤信息,並告訴我們它被引發的路線。該消息中有問題的方法/選擇器的名稱。 – 2013-03-15 20:45:05

+0

對不起,您確實標記了該行。如果它實際上是一個UITableViewCell,則會出現問題,因爲它應該是MCSwipeTableViewCell。 UITableViewCell無法響應itemLabel。再次,請顯示完整的錯誤消息。 – 2013-03-15 20:47:11

回答

1

Aparently你的UITextField的超視圖的超視圖的類型是UITableViewCell,而不是你期望的MCSwipeTableViewCell對象。查看單元格的創建位置,最有可能是cellForRowAtIndexPath和/或它是您在inteface構建器/故事板編輯器中完成的原型單元格的類分配。

+0

所以我檢查了我的故事板,並且之前將類設置爲MCSwipeTableViewCell。檢查cellForRowAtIndexPath也沒有真正幫助,因爲我所有的指針使用MCSwipeTableViewCell而不是UITableViewCell – user2175433 2013-03-15 21:01:48

+0

編輯'MCSwipeTableViewCell * cell =(MCSwipeTableViewCell *)textField.superview.superview;'並取出最後一次'superview'調用最終解決了這個問題,我甚至不會想到沒有你的建議,非常感謝! – user2175433 2013-03-15 21:11:15

+0

啊,現在我看到錯誤信息,它甚至可以清楚地知道有一個sup erview很多。我們怎麼知道?您可以在文本視圖和單元格之間定期查看。 - 但是,不客氣。樂意效勞。 – 2013-03-15 21:18:42