views:

278

answers:

1

Hi Everyone:

I have a very simple class (shown below), however every time I run it, it returns:

Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key field1.'

I am drawing this nib file to the screen in the following way:

UIViewController *backViewController = [[UIViewController alloc] initWithNibName:@"myViewBack" bundle:[NSBundle mainBundle]];

UIView *backView = backViewController.view;

[self addSubview:backView];

The following is my code for the class that is set to File's Owner:

.h

#import <Foundation/Foundation.h>


@interface myViewBack : NSObject {

    IBOutlet UITextField *field1;
    IBOutlet UITextField *field2;

    IBOutlet UIView *view;
}

@property (nonatomic, retain) IBOutlet UITextField *field1;
@property (nonatomic, retain) IBOutlet UITextField *field2;

@end

.m

#import "myViewBack"


@implementation myViewBack

@synthesize field1;
@synthesize field2;


- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];

    return YES;
}

@end
A: 

What object contains a field called 'username'? You are likely using an object that is not actually the type you think it is. If it is trying to access a field called 'username' on an object that doesn't have that as an instance variable, it will crash.

Try running in debug (Command-Y). When the app crashes, it should stop on the line where it crashed. At this point, bring up the debugger window (Ctrl-Command-Y) and look for the last line in the Threads view (upper left-hand corner in the debugger) that is in black (not gray). This is the last line of your code before it accessed something that doesn't exist. Click on that line and it should show you what object caused the issue.

None of the code you provided specifies 'username' as an instance variable, so it's got to be somewhere else.

Matt Long
Hey Matt: Actually, this "username" field was an old error before I started really simplifying the code to find out what the problem was. The "username" field referenced in the error is now replaced with "field1".
PF1
YOu still should follow Matt's advice: run in debug. See where it crashes. Look at the debugger window... now what object caused the issue?
mahboudz
Apparentally, this line of code caused it to crash: int retVal = UIApplicationMain(argc, argv, nil, nil); located in main.m
PF1
Finally figured it out, it was a issue with my UIView in Interface Builder.
PF1