2012-12-31 83 views
1

我在我的應用程序中使用CoreData,它是一對多的關係,我有兩個實體foldersfiles。所以一個文件夾可以有很多文件。因此,我的第一個視圖控制器顯示所有文件夾,並在每個文件夾上單擊時顯示其中的文件。在CoreData中傳遞對象

現在其中的所有文件夾和文件都是由用戶動態創建的,並非來自任何數據庫或來自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獲得filesArray。

根據以下回答編輯。我創建了一個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]); 
} 

問候 蘭吉特。

+0

編輯您的問題,並寫下您的疑問;) –

+0

檢查此問題,http://stackoverflow.com/questions/14170772/move-rows-to-bottom-in-a-tableview-which-uses-coredata – Ranjit

+0

嗨,現在我有點忙。我會盡快閱讀你的問題。 –

回答

2

如果您分享更多的細節,我們可以幫助你,但在此期間...

如果主控制器顯示了文件夾和細節控制器顯示文件,只需注入選定文件夾中的一個參考細節控制器。

例如,在這樣的(它需要的要合成)的細節控制器創建屬性

@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]; 

其中results將包含在您的商店從任何文件夾indipendently的所有文件。您可以使用此數組來顯示錶中的文件。

如果你有很多文件,我真的建議看看NSFetchedResultsController這個類。它允許延遲加載UITableView

你可以在這裏找到一個很好的教程:How To Use NSFetchedResultsController

編輯3

如何去提醒?

你應該提供給您的要求,您檢索文件,其中提醒不nil(你NSFetchedResultsController使用的)像下面

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

這樣一個謂語。 reminder是您在模型中使用的財產。

P.S.檢查代碼是因爲我編寫的代碼不支持Xcode。

+0

你好,謝謝,爲此,我已經在我的Master和detailViewController中完成了這一切,並且一切正常。現在對一些文件,我設置提醒,以顯示本地通知。所以無論何時彈出通知,它們都是本地通知的委託函數,應該寫入appdelegate.m。他們正在創建此ThirdViewController的一個實例,並打開此ViewController以顯示特定日期和時間的文件。希望這是明確的。現在將這些文件傳遞給這個ThirdViewController。 – Ranjit

+0

希望這清楚,你需要的任何信息。 – Ranjit

+0

@Ranjit我不理解你的評論。你能編輯你的問題並在那裏提供細節嗎?謝謝 –