开发者

Load view with nib file from another nib file

I'm sure this is fairly simple but i cannot make it work.

I have a UIViewController called viewController including a nib which should load up several other views. Four of these other views are views of my class BlockView (subclass of UIView). I've made a nib file for the BlockView class also (by creating a new nib and setting it's file owner to BlockView, is there something else to be done here?). In the viewController I d开发者_运维技巧rag in four UIViews and set their file owners 'Class' to be BlockView.

When I run this I am only seeing four white squares where I put the BlockViews, why is that?

Thanks

-Michael


Short answer: There is no support in IB for what you want to do but there are code workarounds to do what you want.

One of the biggest problems with IB is that the name "File's owner" is a terrible name. Just FYI, a view can never be a File's owner. File's owner is usually a ViewController.

To do what you want to do, look up "Loading Nib Files Using UINib and NSNib" in the docs. That will show you how to load BlockView from BlockView.xib.

Then you will have to use addSubview: in code and set the frame to the right position.

If you leave the 4 placeholder blocks in IB, you can simply set the frame of the loaded BlockViews to the frame of the placeholder views for convenience.


To create an instance of a custom class and add it as a subview to your view controller's view hierarchy:

  1. Add a UIView to your view hierarchy.
  2. In the Identity Inspector (⌥⌘3) set the "Class" attribute to BlockView.

This creates an instance of BlockView, but of course it doesn't know anything about the BlockView.xib where you designed it…

My team recently found an interesting way to solve this problem (embedding a Nib in other Nibs), which I wrote up in our iOS developer blog. The crux is overriding -awakeAfterUsingCoder: in the BlockView class, replacing the object loaded from the "parent" Nib with the one loaded from the "child" Nib (BlockView.xib).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜