为什么我的程序不进入viewDidLoad方法?

时间:2011-08-20 19:27:25

标签: iphone uiview viewdidload

我正在尝试使用Core数据测试此程序。再一次,这是Dave Marks书中的一个例子。它在视图上有四个文本字段,它通过使用核心数据连接到数据库来加载它。

应用程序是作为基于窗口的应用程序创建的,然后我添加了一个viewController。该文件的所有者是我创建的自定义viewController类的子类。

当我执行它时,UIView会出现一个空白视图,没有我在视图中创建的文本框或标签。

我在main方法中放了一个断点,当我点击step into方法按钮时,它甚至不会从那里去。当我在viewDidLoad方法上放置一个断点时,它甚至都没有。

最后我在控制台上没有出现任何错误。发生了什么事?

这是viewController类:

#import "PersistenceViewController.h"
#import "CoreDataPersistenceAppDelegate.h"

@implementation PersistenceViewController


@synthesize line1;
@synthesize line2;
@synthesize line3;
@synthesize line4;


#pragma mark - View lifecycle

- (void)viewDidLoad
{
    CoreDataPersistenceAppDelegate *appDelegate = [[UIApplication sharedApplication]                delegate];
    NSManagedObjectContext *context = [appDelegate managedObjectContext];
    NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Line" inManagedObjectContext:context];
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setEntity:entityDescription];

    NSError *error;
    NSArray *objects = [context executeFetchRequest:request error:&error];
    if (objects == nil) {
        NSLog(@"There was an error");
    }

    for (NSManagedObject *oneObject in objects) {
        NSNumber *lineNum = [oneObject valueForKey:@"lineNum"];
        NSString *lineText = [oneObject valueForKey:@"lineText"];

        NSString *fieldName = [NSString stringWithFormat:@"line%d", [lineNum integerValue]];
        UITextField *theField = [self valueForKey:fieldName];
        theField.text = lineText;
    }
    [request release];

    UIApplication *app = [UIApplication sharedApplication];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillResignActive:) name:UIApplicationWillResignActiveNotification object:app];

    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

    -(void) applicationWillResignActive:(NSNotification *)notification {
        CoreDataPersistenceAppDelegate *appDelegate = [[UIApplication sharedApplication]delegate];
        NSManagedObjectContext *context = [appDelegate managedObjectContext];
        NSError *error;

        for (int i=1; i<=4; i++) {
        NSString *fieldName = [NSString stringWithFormat:@"line%d",i];
        UITextField *theField = [self valueForKey:fieldName];

        NSFetchRequest *request = [[NSFetchRequest alloc]init];

        NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Line" inManagedObjectContext:context];
        [request setEntity:entityDescription];
        NSPredicate *pred = [NSPredicate predicateWithFormat:@"(lineNum = %d)", i];
        [request setPredicate:pred];

        NSManagedObject *theLine = nil;

        NSArray *objects = [context executeFetchRequest:request error:&error];

        if (objects == nil) {
            NSLog(@"There was an error");
        }
        if ([objects count] > 0) 
            theLine = [objects objectAtIndex:0];
        else
            theLine = [NSEntityDescription insertNewObjectForEntityForName:@"Line" inManagedObjectContext:context];

        [theLine setValue:[NSNumber numberWithInt:i] forKey:@"lineNum"];
        [theLine setValue:theField.text forKey:@"lineText"];

        [request release];
    }
    [context save:&error];
}

- (void)viewDidUnload
{
    self.line1 = nil;
    self.line2 = nil;
    self.line3 = nil;
    self.line4 = nil;

    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)dealloc
{
    [line1 release];
    [line2 release];
    [line1 release];
    [line1 release];

    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}


@end    

1 个答案:

答案 0 :(得分:1)

请务必使用正确的nib更改在appdelegate中初始化的viewcontroller。空白的UIView可能是您启动项目时创建的视图。

在AppDelegate中有一个名为applicationdidFinishLaunchingWithOptions的方法,其中包含以下代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.viewController = [[RIViewController alloc] initWithNibName:@"RIViewController" bundle:nil];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}

在我的情况下,我加载RIViewController。默认情况下,nib文件与控制器具有相同的名称,因此也称为RIViewController。