2012-07-24 65 views
2

我想創建一個使用核心數據的iphone應用程序。 據我所知,只有master-detail應用程序模板給了我一個使用Core Data的選項。但它創建了表格視圖。沒有主數據的核心數據詳細模板

我想使用的是視圖控制器不是表視圖控制器。 我不能使用單核視圖應用程序模板的核心數據。

我應該採用哪種方式來解決此問題?

謝謝。

+0

這裏更換名剛剛從包中刪除不需要的文件,添加新的'UIViewController'你的包,最後修改'AppDelegate.m'文件加載你的新'的UIViewController '和_violá_,它完成了。 :)或者...你可以使用'CoreData'創建一個新的應用程序,並且將'AppDataGate'的'AppDelegate.m'和'AppDelegate.h'的相關部分複製到另一個沒有'CoreData'的項目中'UIViewController',添加'CoreData'文件和_violá_,你已經完成了。 :) – holex 2012-07-24 13:06:23

+0

正如我已經實施了很多事情,如果我能夠做到這一點,你的第一個選擇將是非常好的。我不確定我應該在Appdelegate.m中更改什麼?謝謝 – Ataman 2012-07-24 13:22:58

+0

將這些方法添加到AppDelegate.m文件中:'-saveContext','-managedObjectContext','-managedObjectModel'和'persistentStoreCoordinator'並且請確保這個文件有'-applicationDocumentsDirectory'方法。將'readonly'屬性添加到'NSManagedObjectContext','NSManagedObjectModel','NSManagedObjectModel'和'#import '的'AppDelegate.h'文件中,並確保您的應用程序將' CoreData.framework'。 – holex 2012-07-24 13:31:50

回答

5

你應該認識到,CoreData是一個框架,不綁定到像UITableView這樣的任何UIKit組件。您可以在任何類型的應用程序中自由使用它。您所要做的就是創建管理CoreData操作的單例類,並將CoreData.framework添加到您的項目中。

這裏是我的DataAccessLayer模板:

DataAccessLayer.h

#import <Foundation/Foundation.h> 
#import <CoreData/CoreData.h> 
@interface DataAccessLayer : NSObject 

@property (strong, nonatomic) NSManagedObjectContext *managedObjectContext; 
@property (strong, nonatomic) NSManagedObjectModel *managedObjectModel; 
@property (strong, nonatomic) NSPersistentStoreCoordinator *storeCoordinator; 

+ (DataAccessLayer *)sharedInstance; 
- (void)saveContext; 

@end 

DataAccessLayer.m

#import "DataAccessLayer.h" 
@interface DataAccessLayer() 
- (NSURL *)applicationDocumentsDirectory; 
@end 

@implementation DataAccessLayer 
@synthesize storeCoordinator; 
@synthesize managedObjectModel; 
@synthesize managedObjectContext; 

+ (DataAccessLayer *)sharedInstance { 
    __strong static DataAccessLayer *sharedInstance = nil; 
    static dispatch_once_t onceToken; 
    dispatch_once(&onceToken, ^{ 
    sharedInstance = [[DataAccessLayer alloc] init]; 
    sharedInstance.storeCoordinator = [sharedInstance persistentStoreCoordinator]; 
    sharedInstance.managedObjectContext = [sharedInstance managedObjectContext]; 
    }); 
    return sharedInstance; 
} 

#pragma mark - Core Data 

- (void)saveContext { 
    @synchronized(self) { 
    NSError *error = nil; 
    if (managedObjectContext != nil) 
    { 
     if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) 
     { 
     NSLog(@"error: %@", error.userInfo); 
     /* 
     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. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button. 
     */ 
     NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Oops!" 
                 message:@"Something has gone terribly wrong! You need to reinstall the app in order for it to work properly." 
                 delegate:nil 
               cancelButtonTitle:@"Close." 
               otherButtonTitles:nil, nil]; 
     [alert show]; 
     } 
    } 
    } 
} 

#pragma mark Core Data stack 

/** 
Returns the managed object context for the application. 
If the context doesn't already exist, it is created and bound to the persistent store coordinator for the application. 
*/ 
- (NSManagedObjectContext *)managedObjectContext { 
    if (managedObjectContext != nil) 
    { 
    return managedObjectContext; 
    } 

    if (storeCoordinator != nil) 
    { 
    managedObjectContext = [[NSManagedObjectContext alloc] init]; 
    [managedObjectContext setPersistentStoreCoordinator:storeCoordinator]; 
    } 
    return managedObjectContext; 
} 

/** 
Returns the managed object model for the application. 
If the model doesn't already exist, it is created from the application's model. 
*/ 
- (NSManagedObjectModel *)managedObjectModel { 
    if (managedObjectModel != nil) 
    { 
    return managedObjectModel; 
    } 
    NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"DataModel" withExtension:@"momd"]; 
    managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];  
    return managedObjectModel; 
} 

/** 
Returns the persistent store coordinator for the application. 
If the coordinator doesn't already exist, it is created and the application's store added to it. 
*/ 
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator { 
    if (storeCoordinator != nil) 
    { 
    return storeCoordinator; 
    } 

    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"words_db.sqlite"]; 

    NSError *error = nil; 
    storeCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]]; 
    if (![storeCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&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. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button. 

    Typical reasons for an error here include: 
    * The persistent store is not accessible; 
    * The schema for the persistent store is incompatible with current managed object model. 
    Check the error message to determine what the actual problem was. 


    If the persistent store is not accessible, there is typically something wrong with the file path. Often, a file URL is pointing into the application's resources directory instead of a writeable directory. 

    If you encounter schema incompatibility errors during development, you can reduce their frequency by: 
    * Simply deleting the existing store: 
    [[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil] 

    * Performing automatic lightweight migration by passing the following dictionary as the options parameter: 
    [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil]; 

    Lightweight migration will only work for a limited set of schema changes; consult "Core Data Model Versioning and Data Migration Programming Guide" for details. 

    */ 
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Oops!" 
                message:@"Something has gone terribly wrong! You need to reinstall the app in order for it to work properly." 
                delegate:nil 
              cancelButtonTitle:@"Close." 
              otherButtonTitles:nil, nil]; 
    [alert show]; 
    }  

    return storeCoordinator; 
} 

#pragma mark Application's Documents directory 

/** 
Returns the URL to the application's Documents directory. 
*/ 
- (NSURL *)applicationDocumentsDirectory { 
    return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]; 
} 


@end 

您還必須以創建自己的數據來創建一個.xcdatamodeld文件模型對象。並以適當

NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"DataModel" withExtension:@"momd"]; 
+0

我照你說的去做了。然後將我現有的所有代碼複製到新創建的應用程序中。沒有生成錯誤。但是當我運行它時,我得到運行時錯誤,說'+ entityForName:找不到實體名稱'UserData'的NSManagedObjectModel''UserData是我的核心數據中的實體名稱。你知道爲什麼會發生這種情況嗎?謝謝! – Ataman 2012-07-24 16:44:43

+0

我在以前的應用程序中做的是在同一個類中創建managedobjectcontext,所以我使用NSManagedObjectContext * context = [self managedObjectContext];但現在我想我們正在datalayeraccess類中創建它,所以相反,如果我自己managedobjectcontext應該使用別的東西? – Ataman 2012-07-24 16:51:54

+0

最後做到了。感謝您的指導! – Ataman 2012-07-24 17:22:21