0

I have tried several other examples, on this site and others, but for the life of me I cannot get this to work.

I have a NIB file which is a part of the class "ViewController2". I need to use a few of the views in this NIB file in my "ViewController1" class.

Each time I call

[[NSBundle mainBundle]loadNibNamed:@"ViewController2" owner:self options:nil];

it causes a crash, saying that one of my UIButtons is not KVC compliant, but I have properly linked all the buttons and outlets, to no avail.

Any help would be extremely appreciated! Thank you in advance!

holex
  • 23,961
  • 7
  • 62
  • 76
Will
  • 15
  • 3
  • Will the suggestion on the accepted answer of this question work for you? http://stackoverflow.com/questions/5849330/one-xib-file-with-multiple-files-owners – trojanfoe Aug 25 '13 at 07:49
  • I tried that trojanfoe, but after entering the following code: VC2 *VC2 = [[VC2 alloc]init]; [self addChildViewController:VC2]; [_containerView addSubview:advancedMenu.stopwatchView]; I get the following error: *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key customAboutBtn.' – Will Aug 26 '13 at 11:03
  • the `owner` should be `nil`. – holex Aug 26 '13 at 20:14

2 Answers2

0

that is faster at least 3 times than NSBundle:

ViewController2 *_viewController2 = [[[UINib nibWithNibName:@"ViewController2" bundle:nil] instantiateWithOwner:nil options:nil] objectAtIndex:0];

it will load the nib file and it will create a new instance of the ViewController2 class.


NB: if you'd like to use an already existing instance of the ViewController2 class for the nib, you will need to set that instance as owner.

holex
  • 23,961
  • 7
  • 62
  • 76
0

When you load a nib and set the owner: property to self, iOS tries to wire up the outlets with KVC. If you don't have a UIButton with a keyPath that matches the one in the xib file inside of the class you're currently in, you'll get the crash. You need to set owner: to nil.

Daddy
  • 9,045
  • 7
  • 69
  • 98