此代码在一个应用程序中完美运行,在另一个应用程序中崩溃并带有消息:
[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 :(得分:0)
好的,问题出在第二个视图控制器中。 我偶尔发现它。
我将自定义视图UIBarButtonItem
用作导航栏titleView
。