viewController 视图未加载
Posted
技术标签:
【中文标题】viewController 视图未加载【英文标题】:viewController view not loaded 【发布时间】:2011-06-19 13:33:17 【问题描述】:我为 iphone 创建了一个基于窗口的项目;我想从核心数据开始。 我添加了一个 viewController 并创建了所有连接(我认为正确!) 在代表中时,在 didFinishLaunchingWithOptions 我尝试显示我的视图没有任何反应;当我启动应用程序时,它只显示主窗口。
代码是:
//
// 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
【问题讨论】:
代码太多。请阅读sscce.org和***.com/help/mcve 【参考方案1】:您应该在 IB 中仔细检查您的 viewController 插座是否连接到 CoreDataViewController 的实际实例。
【讨论】:
我查过了!在 MainWindow.xib 我有一个 viewController 对象(CoreDataViewController)和一个委托对象(ProvaCoreDataAppDelegate);第一个是与 ProvaCoreDataAppDelegate.h 中定义的 viewController 属性的连接(引用插座),第二个是其插座 viewController 和 CoreDataViewController 对象之间的连接(Outlet) 另一个提示!应用启动时不会调用我的 coreDataViewController 对象的 viewDidLoad 方法;只显示主窗口! ..我不明白为什么!我试图将我的项目与从基于视图的模板生成的项目进行比较,似乎没有区别! nib 中的 viewController 对象上的视图出口是否连接到您要显示的视图?此外,请检查以确保笔尖是您正在构建的目标的一部分。 第一个问题的答案是肯定的!但是,对不起,我怎样才能看到笔尖是否在目标中? 在 Xcode 4 中,选择 nib,如果最右边的窗格尚未显示,则显示它,然后查看 File Attributes Inspector。在 Target Membership 组中,确保选中您当前的目标。以上是关于viewController 视图未加载的主要内容,如果未能解决你的问题,请参考以下文章
PF QueryCollection ViewController 中首次加载时未出现图像
将 customTableView 添加到未正确加载 json 数据的视图上
iPhone 抖动问题:加载 viewController 时未调用 viewDidAppear