2013-05-15 35 views
0

此代碼工作完全在另外一個應用程序,並崩潰,並留言:[的UIBarButtonItem _isAncestorOfFirstResponder]:無法識別的選擇發送到實例

[UIBarButtonItem _isAncestorOfFirstResponder]: unrecognized selector sent to instance 

如果我評論的神奇記錄代碼,我可以看到,方法運行正常。

- (void)viewDidLoad{ 
    [super viewDidLoad]; 

    UIBarButtonItem *addButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addNote:)]; 
    self.navigationItem.rightBarButtonItem = addButton; 
} 


- (IBAction)addNote:(id)sender{ 
    NSLog(@"%s",__PRETTY_FUNCTION__); 

    NSManagedObjectContext *localContext = [NSManagedObjectContext MR_contextForCurrentThread]; 
    Note *note = [Note MR_createInContext:localContext]; 
    NSDate *now = [NSDate date]; 
    note.date = now; 
    note.person = _person; 
    [localContext MR_saveWithOptions:MRSaveSynchronously completion:^(BOOL success, NSError *error) { 

     UIStoryboard *sb = [UIStoryboard storyboardWithName:NSBundle.mainBundle.infoDictionary[@"UIMainStoryboardFile"] bundle:NSBundle.mainBundle]; 
     NoteViewController *vc = [sb instantiateViewControllerWithIdentifier:@"NoteViewController"]; 
     vc.note = note; 

     [self.navigationController pushViewController:vc animated:YES]; 
    }]; 
} 


- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 

    UILabel *noResultsLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.tableView.bounds.size.width, self.tableView.bounds.size.width)]; 
    noResultsLabel.textColor = [UIColor colorWithWhite:0.557 alpha:1.000]; 
    noResultsLabel.font = [UIFont boldSystemFontOfSize:35]; 
    noResultsLabel.textAlignment = NSTextAlignmentCenter; 
    noResultsLabel.text = NSLocalizedString(@"No notes", nil); 
    self.tableView.nxEV_emptyView = noResultsLabel; 

    self.fetchedResultsController.delegate = self; 
    [self.fetchedResultsController performFetch:nil]; 
    [self.tableView reloadData]; 
} 

- (void)viewWillDisappear:(BOOL)animated 
{ 
    [super viewWillDisappear:animated]; 
    _fetchedResultsController.delegate = nil; 
} 

#pragma mark - Fetched results controller 
- (void)dealloc 
{ 
    NSLog(@"%s",__PRETTY_FUNCTION__); 
    self.fetchedResultsController.delegate = nil; 
    self.fetchedResultsController = nil; 
} 

- (NSFetchedResultsController *)fetchedResultsController 
{ 
    if (_fetchedResultsController != nil) { 
     return _fetchedResultsController; 
    } 

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"person == %@", _person]; 

    self.fetchedResultsController = [Note MR_fetchAllGroupedBy:@"source" withPredicate:predicate sortedBy:@"source,date" ascending:YES delegate:self]; 

    NSError *error = nil; 
    if (![self.fetchedResultsController performFetch:&error]) { 
     NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
     abort(); 
    } 

    return _fetchedResultsController; 
} 

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller 
{ 
    [self.tableView beginUpdates]; 
} 

- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo 
      atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type 
{ 
    switch(type) { 
     case NSFetchedResultsChangeInsert: 
      [self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade]; 
      break; 

     case NSFetchedResultsChangeDelete: 
      [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade]; 
      break; 
    } 
} 

- (void)selectCreatedRowatIndexPath:(NSIndexPath *)indexPath{ 
    [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle]; 
} 
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject 
     atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type 
     newIndexPath:(NSIndexPath *)newIndexPath 
{ 
    UITableView *tableView = self.tableView; 

    switch(type) { 
     case NSFetchedResultsChangeInsert: 
      [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; 
      break; 

     case NSFetchedResultsChangeDelete: 
      [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; 
      break; 

     case NSFetchedResultsChangeUpdate: 
      [self configureCell:(NoteCell *)[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath]; 
      break; 

     case NSFetchedResultsChangeMove: 
      [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; 
      [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]withRowAnimation:UITableViewRowAnimationAutomatic]; 
      break; 
    } 
} 

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller 
{ 
    [self.tableView endUpdates]; 
    [self.tableView reloadData]; 
} 

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ 
    id <NSFetchedResultsSectionInfo> sectionInfo = [self.fetchedResultsController sections][section]; 
    if (sectionInfo.name.intValue == 0) { 
     return NSLocalizedString(@"Not Secret", @"Notes"); 
    }else return NSLocalizedString(@"Secret", @"Notes"); 
} 


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return [[self.fetchedResultsController sections] count]; 
} 


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section]; 
    return [sectionInfo numberOfObjects]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Note Cell"; 

    NoteCell *cell = (NoteCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 
    [self configureCell:cell atIndexPath:indexPath]; 

    return cell; 
} 
- (void)configureCell:(NoteCell *)cell atIndexPath:(NSIndexPath *)indexPath 
{ 
    Note *note = [self.fetchedResultsController objectAtIndexPath:indexPath]; 

    cell.dateLabel.text = [[CZDateFormatterCache mainQueueCache] localizedStringFromDate:note.date dateStyle:kCFDateFormatterLongStyle timeStyle:kCFDateFormatterShortStyle]; 

    if (note.note.length > 0) { 
     cell.noteLabel.text = note.note; 
    } else { 
     cell.noteLabel.text = NSLocalizedString(@"New Note", nil); 
    } 

    cell.rateView.rate = [note.trustworthy intValue]; 
    NSLog(@"note.trustworthy intValue %i", [note.trustworthy intValue]); 

    cell.rateView.backgroundColor = [UIColor clearColor]; 
    cell.backgroundColor = [UIColor whiteColor]; 
} 

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     [MagicalRecord saveUsingCurrentThreadContextWithBlock:^(NSManagedObjectContext *localContext) { 
      [[self.fetchedResultsController objectAtIndexPath:indexPath] MR_deleteInContext:localContext]; 
     } completion:^(BOOL success, NSError *error) { 
      // 
     }]; 
    } 
} 

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return NO; 
} 

#pragma mark - Segue 
- (void)prepareForSegue:(UIStoryboardSegue *)segue 
       sender:(id)sender { 
    if ([segue.identifier isEqualToString:@"Note Segue"]) { 
     NSLog(@"%s",__PRETTY_FUNCTION__); 
     NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; 
     Note *note = [[self fetchedResultsController] objectAtIndexPath:indexPath]; 
     NoteViewController *anvc = segue.destinationViewController; 
     anvc.note = note; 
    } 
} 
+0

我認爲在推新控制器時存在問題。你在使用ARC嗎?你能發表一些其他的細節嗎? –

+0

@flexaddicted是的,問題主要出現在我推控制器的時候,然而我發現即使沒有它,也很少碰撞。是的,我使用ARC。我會更新我的帖子。 – Shmidt

+0

@flexaddicted我發佈了所有的方法。 – Shmidt

回答

0

好的,問題出在第二個視圖控制器。 我偶爾發現它。

我用UIBarButtonItem自定義視圖作爲導航欄titleView

相關問題