Loading custom UIView from nib, all subviews contained in nib are nil?

You may be misunderstanding how nib loading works. If you define a custom UIView and create a nib file to lay out its subviews you can't just add a UIView to another nib file, change the class name in IB to your custom class and expect the nib loading system to figure it out. You need to modify initWithCoder of your custom UIView class to programmatically load the nib that defines its subview layout.

E.g.

Up vote 1 down vote favorite share g+ share fb share tw.

I have a custom UIView object with a nib that defines the view/subview layout. My outlets are connected, and when I init the object from initWithFrame: everything is in order. The problem I'm having is when I'm using IB to embed the UIView into another nib; the custom UIView appears, but none of the subviews it contains appear - in fact their symbols all resolve to nil.

I have a very minimal initWithCoder: and awakeFromNib: implementation (just does logging) and my understanding is that as the nib is deserialized the subviews should at least be initialized during the process? The only conclusion I'm coming to on my own is that one of two things is happening: either my outlet references are corrupt/bad and aren't working, or my understanding of load-from-nib process is faulty and I'm missing something. Thanks in advance!

Edit: (Code posted for Nekto as requested... as you'll see, it does logging and thats it, heh. ) - (id)initWithCoder:(NSCoder *)aDecoder { if ((self = super initWithCoder:aDecoder)) { NSLog(@"ThumbnailGridView. InitWithCoder frame= %f, %f", self.frame.size.

Width, self.frame.size. Height); } return self; } - (void)awakeFromNib { NSLog(@"ThumbnailGridView:awakeFromNib"); } Edit 2: Nibs, controllers, subviews, etc. Nibs: I have a single nib containing a UIView. This UIView contains a single UIScrollView, which is filled with a grid of UIViews that are defined in another nib.

(Note: this part works fine, as the fill is done programmatically and works with an initWithFrame: call. ) The problem here is that the UIScrollView symbol is nil after initWithCoder: and awakeFromNib: are both called, so objects are just being added to nil and nothing happens. If the scrollview symbol was not nil, I'm sure this would work.

Controllers: There are two controllers that utilize this, one is done with initWithFrame: and works perfectly, the other embeds it as a nib-based reference. (As mentioned elsewhere here, defining a UIView in IB, setting the custom class. ) I stepped through the code, and that view -is- being initialized properly - only its subviews are "missing".

Does this help give a clearer picture of the situation at all? Iphone ios uiview interface-builder nib link|improve this question edited Sep 28 '11 at 20:32 asked Sep 28 '11 at 18:52strahlr84.

We can help you only if you will post some code. You can start from place where you are creating your custom subview, initWithFrame: and others. – Nekto Sep 28 '11 at 19:35 I should note that my custom subview is actually just defined in the IB nib for the viewcontroller managing it.

(Set a UIView placement, change its custom class, etc.) – strahlr Sep 28 '11 at 20:06 I don't think the problem is in your Objective C code (other than the missing call to super awakeFromNib). We need to know what nib files you have in your project, what viewcontrollers and views are defined in each nib and how you are 'embedding' your custom view in another nib. – Robin Summerhill Sep 28 '11 at 20:11.

You may be misunderstanding how nib loading works. If you define a custom UIView and create a nib file to lay out its subviews you can't just add a UIView to another nib file, change the class name in IB to your custom class and expect the nib loading system to figure it out. You need to modify initWithCoder of your custom UIView class to programmatically load the nib that defines its subview layout.

E.g. : - (id)initWithCoder:(NSCoder *)aDecoder { if ((self = super initWithCoder:aDecoder)) { NSBundle mainBundle loadNibNamed:@"CustomView" owner:self options:nil; self addSubview:self. ToplevelSubView; } return self; } Your custom view's nib file needs to have the 'File's owner' class set to your custom view class and you need to have an outlet in your custom class called 'toplevelSubView' connected to a view in your custom view nib file that is acting as a container for all the subviews.

Add additional outlets to your view class and connect up the subviews to 'File's owner' (your custom UIView). Alternatively, just create your custom view programmatically and bypass IB.

Thanks Robin, this cleared up both my uncertainty and my problem completely; I had thought the call to mainBundle was implicit, and was doing it explicitly in my initWithFrame: methods. This resolved everything, many thanks again! – strahlr Sep 29 '11 at 6:02.

Check that you are calling the 'super' implementation of initWithCoder: and awakeFromNib in each of your overridden methods i.e. - (id)initWithCoder:(NSCoder *)decoder { if ((self = super initWithCoder:decoder)) { ...your init code... } return self; } - (void)awakeFromNib { super awakeFromNib; ...your init code... }.

Managed to forget the super awakefromNib; call though adding it still didn't give me the valid references to objects. I'm still concerned I'm mistakenly assuming that subviews defined in the nib are auto-created... despite that notion being a bit counter-intuitive what with outlets being there. – strahlr Sep 28 '11 at 20:02.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions