English 中文(简体)
iOS核心数据-用于将上下文传递到细节视图的设计模式
原标题:iOS Core Data - Design Pattern for Passing Context to Detail Views

我有点困惑于一个问题,我认为这个问题可以追溯到我的设计。

我正在基于一组主要是静态的行(4)构建一个TableViewController,并将其作为UITableView的基础。每一行都将启动不同的视图(详细信息和UITableViews)。。。在我的顶部视图的托管对象上下文中,我可以很容易地导航到相关的详细视图,因为它在上下文中(昵称)。。我最初考虑的是简单地使用不相关的表和一系列按钮来启动视图。。。但我放弃了这个想法。我目前的主要问题是知道如何切换MOC或切换到不同的fetchedresults控制器(顺便说一下,这些控制器是在each.m中定义的,用于从不同的表中提取)。

目前,我在TableViewControler.m中有一个开关

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSInteger switchval = indexPath.row;

    switch (switchval) {
     case 0: // Nickname 
     {
       //This version brings up a detailed view controller - 
      NickNameDetail *controller = [[NickNameDetail alloc] initWithNibName:@"NickNameDetail" bundle:nil];
      controller.fetchedResultsController = self.fetchedResultsController;
      controller.managedObjectContext = [self.fetchedResultsController objectAtIndexPath:indexPath];
      [self.navigationController pushViewController:controller animated:YES];
      break;  
     }
     case 1:  
     {      
      //NextTableViewContoller *.... (etc)... here here
    ...

然而,我不知道如何切换到不同的上下文并获得不同的行。

meTableViewController.managedObjectContext = self.managedObjectContext;
nickNameDetail.managedObjectContext = self.managedObjectContext;
nextTableViewController.managedObjectContext = self.managedObjectContext;

以前有人遇到过这样的情况吗?很抱歉,无法使代码格式化程序正常工作。

谢谢

最佳回答

在某些情况下,将当前视图控制器的MOC传递给导航堆栈上推送的新视图控制器可能很好/很合适。但是,您通常希望通过新创建的MOC。请按照以下步骤执行操作:

在您的应用程序中,委托添加以下方法

- (NSManagedObjectContext*)createNewManagedObjectContext
{

    NSManagedObjectContext *moc = [[NSManagedObjectContext alloc] init]; 
    [moc setPersistentStoreCoordinator:[self persistentStoreCoordinator]]; 
    return [moc autorelease];
}

在您看来,管制员按如下方式通过主运行中心

myAppDelegate *mainDelegate = (myAppDelegate *)[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *mainMOC = [mainDelegate createNewManagedObjectContext]; 
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(contextDidSave:) 
                                                         name:NSManagedObjectContextDidSaveNotification
                                                       object:mainMOC];

newViewController.managedObjectContext = mainMOC;

然后根据需要处理通知,下面是一个示例

- (void)contextDidSave:(NSNotification *)notification
{

    [managedObjectContext mergeChangesFromContextDidSaveNotification:notification];
    [self.tableView reloadData];

}

您还需要为每个视图控制器定义并使用不同的NSFetchedResultsController。这是因为,对于每个视图控制器(不同的实体、谓词等),要获取和显示的数据当然是不同的。只需在每个实现文件中定义它们。以下是一个示例:

- (NSFetchedResultsController *)fetchedResultsController {

    if (fetchedResultsController != nil) {
        return fetchedResultsController;
    }

    /*
     Set up the fetched results controller.
     */

    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Context" inManagedObjectContext:managedObjectContext];
    [request setEntity:entity];

    if(self.project){
        // get the contexts for the project
        NSPredicate * predicate = [NSPredicate predicateWithFormat: @"projectName == %@", self.project.name];
        [request setPredicate:predicate];
    }

    // Edit the sort key as appropriate.
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES selector:@selector(caseInsensitiveCompare:)];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
    [request setSortDescriptors:sortDescriptors];


    // Edit the section name key path and cache name if appropriate.
    // nil for section name key path means "no sections".
    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:nil];
    aFetchedResultsController.delegate = self;
    self.fetchedResultsController = aFetchedResultsController;

    [aFetchedResultsController release];
    [request release];
    [sortDescriptor release];
    [sortDescriptors release];

    return fetchedResultsController;
}    

然后根据需要使用fetchedResultsController。例如,将其放入viewDidLoad方法中:

    NSError *error;
    if (![[self fetchedResultsController] performFetch:&error]) {
        // Handle error
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        exit(-1);  // Fail
    }
问题回答

暂无回答




相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签