[UIBarButtonItem _isAncestorOfFirstResponder]:发送到实例的无法识别的选择器
Posted
技术标签:
【中文标题】[UIBarButtonItem _isAncestorOfFirstResponder]:发送到实例的无法识别的选择器【英文标题】:[UIBarButtonItem _isAncestorOfFirstResponder]: unrecognized selector sent to instance 【发布时间】:2013-05-15 08:04:10 【问题描述】:此代码在一个应用程序中完美运行,并在另一个应用程序中崩溃并显示消息:
[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;
【问题讨论】:
我认为当你推送你的新控制器时有问题。你在用ARC吗?你能发布一些其他细节吗? @flexaddicted 是的,这个问题主要是在我按下控制器时出现的,但是我发现即使没有它,它也很少会崩溃。是的,我使用 ARC。我会更新我的帖子。 @flexaddicted 我发布了我在其中的所有方法。 【参考方案1】:好的,问题出在第二个视图控制器中。 偶然发现的。
我使用UIBarButtonItem
和自定义视图作为导航栏titleView
。
【讨论】:
以上是关于[UIBarButtonItem _isAncestorOfFirstResponder]:发送到实例的无法识别的选择器的主要内容,如果未能解决你的问题,请参考以下文章