0
votes

I face some problem in creating new application using xib and objective-C. I am using xib instead of storyboard. Every time I created new project, added some label and button, then It worked well. It showed all object that I attached.

Problem starts when I declare them on my viewController class, and connect them with the object

Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UIViewController 0x7aa91da0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key firstLabel.'

And this is my File's Owner connection view of my .xib

File's Owner connection view

Here is my ViewController.m that contain outlet and action I declared

#import "ViewController.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UILabel *firstLabel;

- (IBAction)firstButton:(id)sender;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)firstButton:(id)sender {
    NSLog(@"worked!");
}
@end

I am forced to use .xib in Xcode 7 that apple already left behind instead of the powerful storyboard. Any help would be appreciated. Thank you!

2
Can you show where you init ViewController to use?tuledev
Already solved, anhtu. Thank you for your help!Sonic Master

2 Answers

0
votes

Sorry, already found the answer here

I should edit AppDelegate.m by importing my xib header class (ViewController.h), then when create instance for my class I should use that class. This code worked like a charm

// create instance for xib
ViewController *viewController = [[[ViewController alloc] init] initWithNibName:@"ViewController" bundle:nil];

Thank you!

0
votes

In xib file somewhere yet there left a connection with "firstLabel" variable which does not exist in you .h file.

In Spite i see all ok in you code. This to ensure and resolve this crash please remove connections of "firstLabel" and connect again. I hope crash will solve.

Moreover, The crash clearly say only with "firstLabel" variable but nothing else.