ios - viewController View 未加载

标签 ios ios4 core-data

我为 iphone 创建了一个基于窗口的项目;我想从核心数据开始。 我添加了一个 viewController 并创建了所有连接(我认为正确!) 在委托(delegate)中,在 didFinishLaunchingWithOptions 中,我尝试显示我的 View 没有任何反应;当我启动应用程序时,它只显示主窗口。

代码是:

//
//  ProvaCoreDataAppDelegate.h
//  ProvaCoreData
//
//  Created by dvd on 17/06/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import <UIKit/UIKit.h>

@class CoreDataViewController;

@interface ProvaCoreDataAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
    CoreDataViewController *viewController;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet CoreDataViewController *viewController;

@property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, retain, readonly) NSManagedObjectModel *managedObjectModel;
@property (nonatomic, retain, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator;


- (void)saveContext;
- (NSURL *)applicationDocumentsDirectory;

@end


//
//  ProvaCoreDataAppDelegate.m
//  ProvaCoreData
//
//  Created by dvd on 17/06/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "ProvaCoreDataAppDelegate.h"
#import "CoreDataViewController.h"

@implementation ProvaCoreDataAppDelegate


@synthesize window;
@synthesize viewController;

@synthesize managedObjectContext=__managedObjectContext;

@synthesize managedObjectModel=__managedObjectModel;

@synthesize persistentStoreCoordinator=__persistentStoreCoordinator;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    [self.window addSubview:self.viewController.view];
    [self.window makeKeyAndVisible];
    return YES;
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Saves changes in the application's managed object context before the application terminates.
    [self saveContext];
}

- (void)dealloc
{
    [window release];
    [__managedObjectContext release];
    [__managedObjectModel release];
    [__persistentStoreCoordinator release];
    [viewController release];
    [super dealloc];
}

- (void)awakeFromNib
{
    /*
     Typically you should set up the Core Data stack here, usually by passing the managed object context to the first view controller.
     self.<#View controller#>.managedObjectContext = self.managedObjectContext;
    */
}

- (void)saveContext
{
    NSError *error = nil;
    NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
    if (managedObjectContext != nil)
    {
        if ([managedObjectContext hasChanges] && ![managedObjectContext save:&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.
             */
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            abort();
        } 
    }
}

#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;
    }

    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
    if (coordinator != nil)
    {
        __managedObjectContext = [[NSManagedObjectContext alloc] init];
        [__managedObjectContext setPersistentStoreCoordinator:coordinator];
    }
    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:@"ProvaCoreData" 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 (__persistentStoreCoordinator != nil)
    {
        return __persistentStoreCoordinator;
    }

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

    NSError *error = nil;
    __persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
    if (![__persistentStoreCoordinator 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]);
        abort();
    }    

    return __persistentStoreCoordinator;
}

#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

//
//  CoreDataViewController.h
//  ProvaCoreData
//
//  Created by dvd on 17/06/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import <UIKit/UIKit.h>


@interface CoreDataViewController : UIViewController {

    UIView *view;
    UITextField *nameTF;
    UITextField *addressTF;
    UITextField *phoneTF;
    UILabel *statusLbl;
}
@property (nonatomic, retain) IBOutlet UIView *view;
@property (nonatomic, retain) IBOutlet UITextField *nameTF;
@property (nonatomic, retain) IBOutlet UITextField *addressTF;
@property (nonatomic, retain) IBOutlet UITextField *phoneTF;
@property (nonatomic, retain) IBOutlet UILabel *statusLbl;

- (IBAction)saveData:(id)sender;
- (IBAction)findContact:(id)sender;

@end


//
//  CoreDataViewController.m
//  ProvaCoreData
//
//  Created by dvd on 17/06/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "CoreDataViewController.h"


@implementation CoreDataViewController
@synthesize view;
@synthesize nameTF;
@synthesize addressTF;
@synthesize phoneTF;
@synthesize statusLbl;

- (void)dealloc
{
    [view release];
    [nameTF release];
    [addressTF release];
    [phoneTF release];
    [statusLbl release];
    [super dealloc];
}

#pragma mark - View lifecycle

- (void)viewDidUnload
{
    [view release];
    view = nil;
    [self setNameTF:nil];
    [self setAddressTF:nil];
    [self setPhoneTF:nil];
    [self setStatusLbl:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (IBAction)saveData:(id)sender {
}

- (IBAction)findContact:(id)sender {
}
@end

最佳答案

您应该在 IB 中仔细检查您的 viewController socket 是否连接到 CoreData ViewController 的实际实例。

关于ios - viewController View 未加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6402645/

相关文章:

ios - 不断改变重力方向使物体随机移动

ios - 核心数据删除与重置

ios - 将核心数据与图像或按钮(或除 tableView 之外的任何内容)一起使用

iphone - 在 xcode 中安装 OpenCV

ios - 如何处理 dispatch_queue_t dispatch_get_local_queue dispatch_queue_create EXC BAD ACCESS?

iphone - 核心数据——如何插入数组

ios - 在更新期间更改 UITableViewCells 时 swift 崩溃

ios - 重新创建 Quora 应用程序 iOS 7 UISearchBar

ios - 比较多关系中两个属性的 CoreData 谓词

iphone - iOS 上的 WebSocket