1

我在我的应用程序中使用 CoreData,它是一对多的关系,我有两个名为foldersand的实体files。所以一个文件夹可以有很多文件。所以我的第一个视图控制器显示所有文件夹,当单击每个文件夹时,我显示其中存在的文件。

现在,其中存在的所有文件夹和文件都是由用户动态创建的,而不是来自任何数据库或 Web 服务器。

现在我又多了一个视图控制器,我从应用程序委托中打开它,在这里我想显示一些文件。

我知道首先我们应该通过managedObjectContext,它将在应用程序委托中声明

ViewController *View = [[ViewController alloc]initWithNibName: @"ViewController" bundle:nil]
View.managedObjectContext = self.managedObjectContext;

所以如果我们只是通过managedObjectContext,我们可以访问文件实体对象,或者我们也必须传递文件实体对象。

现在如您所知,我有两个实体,称为FolderFile,现在要访问存储在任何文件夹中的文件,我就是这样做的。

NSMutableArray *filesArray = [self.folder.file mutableCopy];

但是现在,我想在anyView中显示我想要的文件,可以二三导航后打开,也可以直接从appDelegate.m打开。

非常清楚,我的问题是如何做到这一点?即我怎样才能从AppDelegate.

基于以下答案编辑。我以这种方式创建了一个 fetchResultsController

- (NSFetchedResultsController *)fetchedResultsController {

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

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    // Edit the entity name as appropriate.
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"File" inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];

    // Set the batch size to a suitable number.
    [fetchRequest setFetchBatchSize:20];

    // Edit the sort key as appropriate.
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"alarm" ascending:YES];
    NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor, nil];

    [fetchRequest setSortDescriptors:sortDescriptors];
    [fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"alarm!= nil"]];

    // 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:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:nil];
    aFetchedResultsController.delegate = self;
    self.fetchResultsController = aFetchedResultsController;

    NSError *error = nil;
    if (![self.fetchResultsController performFetch:&error]) {
        // Replace this implementation with code to handle the error appropriately.
        // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }    
    return  m_fetchResultsController;   
}

在 ViewDidLoad 中,我是这样写的

- (void)viewDidLoad
{
    [super viewDidLoad];
   NSError *error;
    if (![[self fetchedResultsController] performFetch:&error]) {
        // Update to handle the error appropriately.
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        exit(-1);  // Fail
    }


     NSMutableArray *alarms = [[self.fetchResultsController fetchedObjects]mutableCopy];

    NSLog(@"alarmsCount:%d",[alarms count]);
}

问候兰吉特。

4

1 回答 1

2

如果您分享更多详细信息,我们可以为您提供帮助,但与此同时...

如果主控制器显示文件夹而细节控制器显示文件,只需将所选文件夹的引用注入细节控制器即可。

比如像这样在detail控制器中创建一个属性(需要合成)

@property (nonatomic,strong) Folders* currentFolder;

创建细节控制器时,请执行以下操作

DetailController* detCtr = // alloc-init here
detCtr.currentFolder = // the folder you want to pass in

一些笔记

使用骆驼案例表示法。用于NSFetchedResultsController延迟加载与表等相关的数据。重命名实体FilesFolders以其单数形式。

编辑

好的,所以如果你想从应用程序中的任何地方访问你的应用程序委托,你可以调用

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate someMethod];

在这里,您可以使用在 .h 中声明并在 .m 中合成的公共属性检索您感兴趣的文件夹。

现在,这不是一个很好的方法。AppDelegate 类将保持原样。

另一种更优雅的方法是创建一个名为SharedManager(或其他)的单例来存储currentFolder需要可视化的内容。

共享管理器看起来像

//.h
@property (nonatomic,strong) Folder* currentFolder;

+ (SharedManager*)sharedInstance;


//.m
@synthesize currentFolder;

+ (SharedManager*)sharedInstance
{
    static dispatch_once_t once;
    static id sharedInstance;
    dispatch_once(&once, ^{
        sharedInstance = [[SharedManager alloc] init];
    });
    return sharedInstance;
}

也就是说,导入后,它将用于将文件夹设置为当前文件夹

[[SharedManager sharedInstance] setCurrentFolder:theFolderYouWantToShare];

或检索当前文件夹

Folder* theSharedFolder = [[SharedManager sharedInstance] currentFolder];

PS 不要滥用单身人士。我使用的单例模式需要 iOS 4 或更高版本。

编辑 2

是我的错。我不明白这个问题。

如果您需要检索所有文件,您可以创建一个NSFetchRequest如下所示的:

NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"File" inManagedObjectContext:context]; 

NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDescription];

NSError *error = nil; 
NSArray *results = [context executeFetchRequest:request error:&error];

whereresults将包含来自任何文件夹的商店中的所有文件。您可以使用此数组在表中显示文件。

如果你有很多文件,我真的建议你看看NSFetchedResultsControllerclass。它允许与UITableView.

你可以在这里找到一个很好的教程:如何使用 NSFetchedResultsController

编辑 3

如何进行提醒?

您应该向您的请求(与您一起使用的请求NSFetchedResultsController)提供如下谓词

[request setPredicate:[NSPredicate predicateWithFormat:@"reminder != nil"]];

通过这种方式,您可以检索没有提醒的文件nilreminder是您在模型中使用的属性。

PS 检查代码,因为我在没有 Xcode 支持的情况下编写。

于 2012-12-31T12:50:18.267 回答