Failed to load custom view from xib to another xib

I apologize because I see people have asked such questions before. However, I was not lucky to follow the instructions. I am interested in creating a custom view with an xib file and reusing it in another xib file of the view file.

Existing posts:

Using an xib object inside another xib

How to use subclass of xib and UIView together?

I have it already:

  • Created a custom xib file (let it be called CustomView.xib) and the corresponding .h and .m files
  • Set File Owner of xib file to CustomView
  • Created a top-level UIView with other views as child elements (UILabel, etc.)
    • NOTE. It has a child UIView, which is also a custom view written in code.
  • Post IBOutlets
  • Created the ViewController.xib file
  • Added UIView, set the class to CustomView, and also connected to it

This results in an empty view when creating the application.

Then I tried to execute one of the above messages, which was overridden by the CustomView initWithDecoder method and added the following:

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

Unfortunately, this led to the following error:

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

I thought this could be because I did not set the top-level view in CustomView.xib as a CustomView class. So I changed it to CustomView, and then everything went into infinite recursion. This makes sense as it just reboots over and over again.

Not sure what I missed from these previous posts, but I would appreciate any guidance! Thanks!

+6
source share
1 answer
 Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UIView 0x548ff00> setValue:forUndefinedKey:]: 

The reason for the above error usually occurs due to connection failure in Interface Builder , that is, in the UIViewController object with the corresponding objects of the implementation class. Check all connections in Interface Builder . Also check the owner of the file in the xib file.

I hope this solves your problem.

+1
source

Source: https://habr.com/ru/post/914389/


All Articles