Chris
Chris

Reputation: 1034

Created a new empty project in XCode and added a View Controller but it wont compile

Just like the title says, I created a new empty project in xcode that just gave me the AppDelegate header and compile files. I then added a view controller to it and then changed the Main Interface to the name of the viewcontroller xib file. I went ahead and ran the project without adding anything else. I've done this before and it has ran, but this time it is not.

The error message is

'NSUnknownKeyException', reason: 'setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key view.'

I have didn't write any code or changed anything other than what I explained above. The only code in the AppDelegate is the following:

.h

#import <UIKit/UIKit.h>
#import "PopOverViewController.h"
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end

.m

#import "AppDelegate.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.   
    [self.window makeKeyAndVisible];
    return YES;
}
@end

ViewController.h

#import <UIKit/UIKit.h>
@interface PopOverViewController : UIViewController
@end

ViewController.m

#import "PopOverViewController.h"

@interface PopOverViewController ()

@end

@implementation PopOverViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

I've searched through this site but couldn't find anything similar. Every post is about checking the IBOutlets and IBActions and connections but I havent done anything. All I did was like the View to the FileOwner, that's it.

Sorry if this is a repost/duplicate. If you find a page that is useful let me know.

EDIT: Also note that I did try to add the rootViewController (as shown below by Rob) but I still got the same error message.

Upvotes: 0

Views: 3653

Answers (2)

Rob
Rob

Reputation: 438467

When using NIBs, your app delegate would generally look something like:

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

If you create the app using the "Single View" application template versus the "Empty Application" template, you'll have an app delegate that has the appropriate code.

You generally don't have to supply the "Main Interface" field for your XIB unless you have a XIB for your window, too (not to be confused with your XIBs for the views for your view controllers). But XIB-based apps generally use the above didFinishLaunchingWithOptions which eliminates the need for the "Main Interface" to specify a XIB for the window.

Upvotes: 1

Giuseppe Garassino
Giuseppe Garassino

Reputation: 2292

'NSUnknownKeyException', reason: 'setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key view.'

This issue usually appears when you change names to your .h .m files and do not update your .xib file.

Check in .xib file if there are any connection with invalid IBOutlet (right click on File's Owner and look for alert in the black box that appears).

Upvotes: 0

Related Questions