我有一个单独的工作应用程序UITableViewController
;当您按下 Add 按钮时,它会弹出 aModalViewController
并且用户可以将文本添加到文本字段中,按下 save 并通过NSFetchedResultsController
,这将成功更新UITableView
单元格。
我在 TableView 上还有一个搜索按钮,它会调用一个带有搜索选项卡的搜索控制器;我可以输入,按搜索,它会在该表中找到记录。它有一个取消按钮可以返回。
我想修改 UI,使搜索成为 TableView 底部标签栏的一部分并删除按钮。
我的 TableView 有一个prepareForSegue
方法:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"Search"])
{
SearchViewController *searchEntry = (SearchViewController *)segue.destinationViewController;
NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
searchEntry.managedObjectContext = managedObjectContext;
//[self.navigationController presentModalViewController:searchEntry animated:YES];
}
}
所以我基本上是managedObjectContext
穿越了。
现在因为我使用的是 Tab,所以我怀疑没有prepareForSegue
等价物?所以我的问题是,如何managedObjectContext
跨越?
我的SearchViewController
样子是这样的:
- (void)viewDidLoad
{
[super viewDidLoad];
self.searchBar.delegate = self;
self.tView.delegate = self;
self.tView.dataSource = self;
noResultsLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 90, 200, 30)];
[self.view addSubview:noResultsLabel];
noResultsLabel.text = @"No Results";
[noResultsLabel setHidden:YES];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.searchBar becomeFirstResponder];
}
// Once the user taps "search" on the keyboard, you run a fetch and show the results on the table view, or display the No Results label.
// This is one of the SearchBar Delegate methods
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
NSError *error;
if (![[self fetchedResultsController] performFetch:&error])
{
NSLog(@"Error in search %@, %@", error, [error userInfo]);
} else
{
[self.tView reloadData];
[self.searchBar resignFirstResponder];
[noResultsLabel setHidden:_fetchedResultsController.fetchedObjects.count > 0];
}
}
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
id sectionInfo= [[_fetchedResultsController sections] objectAtIndex:section];
return [sectionInfo numberOfObjects];
}
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
Transaction *info = [_fetchedResultsController objectAtIndexPath:indexPath];
[cell.textLabel setText:[NSString stringWithFormat:@"%@", [info valueForKeyPath:@"whoBy.name"]]];
[cell.detailTextLabel setText:[NSString stringWithFormat:@"%@", [info valueForKeyPath:@"occasion.title"]]];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
- (NSFetchedResultsController *)fetchedResultsController
{
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Transaction" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"whoBy.name" ascending:NO];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];
[fetchRequest setFetchBatchSize:20];
NSPredicate *pred = [NSPredicate predicateWithFormat:@"ANY whoBy.name CONTAINS[c] %@", self.searchBar.text];
[fetchRequest setPredicate:pred];
NSFetchedResultsController *theFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:nil];
self.fetchedResultsController = theFetchedResultsController;
_fetchedResultsController.delegate = self;
return _fetchedResultsController;
}
当我运行它时,根本没有任何搜索。如果我将以下代码放在类的顶部,我会在它下面得到错误:
- (NSManagedObjectContext *)managedObjectContext
{
NSManagedObjectContext *context = nil;
id delegate = [[UIApplication sharedApplication] delegate];
if ([delegate performSelector:@selector(managedObjectContext)]) {
context = [delegate managedObjectContext];
}
return context;
}
:
<NSInvalidArgumentException> +entityForName: nil is not a legal NSManagedObjectContext parameter searching for entity name 'Transaction'
所以我基本上是在创建相同的功能,但我想将 managedObjectContext 从 TableView 选项卡栏“传递”到SearchBarViewController
选项卡并按照以前的工作方式进行搜索。
其他相关信息;我在这个搜索文件中有一个NSManagedObjectContext
and的属性。NSFetchedResultsController
任何帮助将不胜感激。
谢谢,