从Segue创建搜索选项卡栏并通过ManagedObjectContext

时间:2013-10-23 10:29:20

标签: ios search core-data tableview nsfetchedresultscontroller

我有一个只有UITableViewController的应用程序;当您按下“添加”按钮时,它会显示ModalViewController,用户可以在文本字段中添加文本,按保存并通过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'

所以我基本上创建了相同的功能,但是我希望将来自TableView标签栏的managedObjectContext“传递”到SearchBarViewController选项卡,然后按照之前的工作方式进行搜索。

其他相关资料;我在此搜索文件中有NSManagedObjectContextNSFetchedResultsController的属性。

任何帮助都会受到大力赞赏。

谢谢,

0 个答案:

没有答案