prepareforsegue方法抛出异常

时间:2014-02-09 07:34:02

标签: ios uitableview core-data

在我的应用中,有一个UITableView显示Core Data个对象。当用户点击一行时,必须显示所选对象的详细视图。详细视图在EditToDoItemViewController中定义。还有一个UIButton可以打开另一个视图来添加新对象AddToDoItemViewController

我正在使用Storyboard。一个segueUIButtonAddToDoItemViewController相关联,其identifieraddToDoItem。另一个segue将单元格连接到EditToDoItemViewController,其identifiereditItem。 这是prepareForSegue方法:

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

    if ([[segue identifier]isEqualToString:@"addToDoItem"]){
        UINavigationController *navigationController = segue.destinationViewController;

        AddToDoItemViewController *addToDoItemViewController = (AddToDoItemViewController*)navigationController.topViewController;
        ToDoItem *addToDoItem = [NSEntityDescription insertNewObjectForEntityForName:@"ToDoItem" inManagedObjectContext:self.managedObjectContext];
        addToDoItem.todoDueDate = [NSDate date];
        addToDoItemViewController.addToDoItem = addToDoItem;

          }
    if ([[segue identifier] isEqualToString:@"editItem"]){
        EditToDoItemViewController *editToDoItemViewController = (EditToDoItemViewController*)self.navigationController.topViewController;
        NSIndexPath *path = [self.tableView indexPathForSelectedRow];
        ToDoItem *editToDoItem = (ToDoItem*)[self.fetchedResultsController objectAtIndexPath:path];
        editToDoItemViewController.editToDoItem = editToDoItem;

    }

}

第一个segue工作正常,但第二个segue投掷了一个预期:

2014-02-09 00:17:31.851 DidIt[474:a0b] -[ToDoItemsTableViewController setEditToDoItem:]: unrecognized selector sent to instance 0x8a90e60
2014-02-09 00:17:31.980 DidIt[474:a0b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ToDoItemsTableViewController setEditToDoItem:]: unrecognized selector sent to instance 0x8a90e60'
*** First throw call stack:
(
    0   CoreFoundation                      0x01ad65e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x018598b6 objc_exception_throw + 44
    2   CoreFoundation                      0x01b73903 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
    3   CoreFoundation                      0x01ac690b ___forwarding___ + 1019
    4   CoreFoundation                      0x01ac64ee _CF_forwarding_prep_0 + 14
    5   DidIt                               0x0000621e -[ToDoItemsTableViewController prepareForSegue:sender:] + 942
    6   UIKit                               0x00afc06c -[UIStoryboardSegueTemplate _perform:] + 156
    7   UIKit                               0x00afc0f9 -[UIStoryboardSegueTemplate perform:] + 115
    8   UIKit                               0x006a9775 -[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:] + 1453
    9   UIKit                               0x006a9924 -[UITableView _userSelectRowAtPendingSelectionIndexPath:] + 279
    10  UIKit                               0x006ad908 __38-[UITableView touchesEnded:withEvent:]_block_invoke + 43
    11  UIKit                               0x005e4183 ___afterCACommitHandler_block_invoke + 15
    12  UIKit                               0x005e412e _applyBlockToCFArrayCopiedToStack + 403
    13  UIKit                               0x005e3f5a _afterCACommitHandler + 532
    14  CoreFoundation                      0x01a9e4ce __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 30
    15  CoreFoundation                      0x01a9e41f __CFRunLoopDoObservers + 399
    16  CoreFoundation                      0x01a7c344 __CFRunLoopRun + 1076
    17  CoreFoundation                      0x01a7bac3 CFRunLoopRunSpecific + 467
    18  CoreFoundation                      0x01a7b8db CFRunLoopRunInMode + 123
    19  GraphicsServices                    0x038e79e2 GSEventRunModal + 192
    20  GraphicsServices                    0x038e7809 GSEventRun + 104
    21  UIKit                               0x005c7d3b UIApplicationMain + 1225
    22  DidIt                               0x0001e02d main + 141
    23  libdyld.dylib                       0x02112725 start + 0
    24  ???                                 0x00000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

我需要你的帮助来确定问题...... 谢谢

1 个答案:

答案 0 :(得分:1)

异常消息

-[ToDoItemsTableViewController setEditToDoItem:]: unrecognized selector ...

告诉你

editToDoItemViewController.editToDoItem = editToDoItem;

行,editToDoItemViewController不是EditToDoItemViewController的实例, 如你所料,但是ToDoItemsTableViewController的一个实例。这看起来不对:

EditToDoItemViewController *editToDoItemViewController =
     (EditToDoItemViewController*)self.navigationController.topViewController;

可能应该是

EditToDoItemViewController *editToDoItemViewController =
     (EditToDoItemViewController*)[segue destinationViewController];