Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Having trouble loading custom view from xib in another xib [duplicate]

Apologies because I see people have asked questions like this before. However, I haven't had much luck following the instructions. I'm interested in creating a custom view with a xib file and reusing it in another view controller's xib file.

Existing Posts:

Using xib object inside another xib

How to use a xib and a UIView subclass together?

I have already:

  • Created a custom xib file (let's call it CustomView.xib), and corresponding .h and .m class files
  • Set the File's Owner of the xib file to CustomView
  • Created a top level UIView with other views as children (UILabel's etc)
    • NOTE this has a child UIView which is a custom view written in code as well
  • Wired up the IBOutlets
  • Created a ViewController.xib file
  • Added a UIView, set the class to CustomView and also wired that up

This results in a blank view showing up when I build the application.

I then tried what one of the above posts said to do which was override CustomView's initWithDecoder method and adding the following:

NSArray *subviewArray = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:self options:nil];
UIView *mainView = [subviewArray objectAtIndex:0];
[self addSubview:mainView];

This unfortunately was resulting in the following error:

Terminating app due to uncaught exception 'NSUnknownKeyException', reason: 
'[<UIView 0x548ff00> setValue:forUndefinedKey:]:

I thought this might be because I didn't set the top level view in CustomView.xib to be of class CustomView. So I then changed it to CustomView, and then things went into infinite recursion. Which makes sense since it's just reloading itself over and over again.

Not sure what I missed from those previous posts, but I'd appreciate any guidance! Thanks!

like image 717
user1328114 Avatar asked Apr 27 '12 23:04

user1328114


1 Answers

Terminating app due to uncaught exception 'NSUnknownKeyException', reason: 
'[<UIView 0x548ff00> setValue:forUndefinedKey:]:

Reason for the above error normally comes due the failure of the connections in the Interface Builder, that is the UIViewController object with the corresponding objects in the implementation class. Please check your all connection in the Interface Builder. Also verify the file owner in the xib file.

I hope this will solve your problem.

like image 177
rejusss Avatar answered Oct 04 '22 02:10

rejusss