7

I have a custom UIView (MyCustomUIView) which is built using Interface Builder. I'd like to place this custom view in MyViewController's view, which is also designed using IB. I've placed an UIView as a subview in MyViewController's XIB and set it's class to MyCustomUIView. The problem is, when I run the code, only a blank view appears. (When I instantiate MyCustomUIView in code, it displays well.)

I'm only overriding the initWithFrame: method the following way in MyCustomUIView.m:

- (id)initWithFrame:(CGRect)frame
{
    [[NSBundle mainBundle] loadNibNamed:@"MyCustomUIView" owner:self options:nil];
    self = self.view;
    return self;
}

What should I do to make the view load properly? How should initWithCoder: look like?

Matt Carrier
  • 5,602
  • 6
  • 26
  • 30
Daniel
  • 73
  • 1
  • 1
  • 6

2 Answers2

2

You are correct. IB uses initWithCoder. initWithCoder should look very similar to your other init methods:

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        // CUSTOM INITIALIZATION HERE
    }
    return self;
}

Once you assign your class within IB, you won't need to instantiate it from the bundle unless I'm misunderstanding your intention.

Joe Trellick
  • 1,125
  • 7
  • 12
  • I've already tried this implementation, but still nothing more than a blank white rectangle appears. Do I have to do anything else in the view controller? I've just placed the UIView and set it's Class attribute. How does Xcode know which xib to look for it? If I insert loadNibNamed in initWithCoder I get an inifinite loop. – Daniel Nov 02 '12 at 17:55
  • You only need to set the class on the uiview object in IB and it should pick it up. The first thing you need to do is to determine if your view is actually appearing. Create a weak reference in your view controller, launch the app, and output the view description. If it's still a plain old UIView, then double check your nib (or storyboard), verify the class name is in the inspector, then save the file for giggles. – Brian Douglas Moakley Nov 02 '12 at 18:09
  • If you aren't using initWithCoder, then delete it from your view class. You don't need to be calling loadNibNamed. IB takes care of that for you. – Brian Douglas Moakley Nov 02 '12 at 18:15
  • Can you have a quick look if I upload a basic demo project? After reading your comments, I'm still not sure what I am doing wrong! – Daniel Nov 02 '12 at 18:18
  • Sure thing ... just post the link and I'll let you know what I see. – Brian Douglas Moakley Nov 02 '12 at 18:23
  • A link to the Xcode project has been added to my question! Please take a look! – Daniel Nov 02 '12 at 19:05
  • 1
    k ... I have the issue solved for you. You were on the right path. I didn't realize you were accessing other nibs. The actual answer can be found here: http://stackoverflow.com/questions/3944964/how-do-i-get-a-view-in-interface-builder-to-load-a-custom-view-in-another-nib ... feel free to view the updated sample project here: http://www.aworldonfire.com/TestProjectUpdated.zip Good luck! – Brian Douglas Moakley Nov 02 '12 at 19:13
0

Do not put anything in the view part of the view controller in IB. Instead, set the nib name for the view controller in IB to the name of the nib containing the view. In the nib containing the view, set the file's owner to the view controller class and hook up its view property to the view.

The result will be that when the view controller is instantiated, if it is instantiated from the nib (which you have not proved is what's really going to happen, but let's just say it is), it will find the nib and load the view from it.

Basically the rule is that it makes no difference where a view controller comes from, it will go through the same steps looking for its view in the same order, as I explain in my book:

http://www.apeth.com/iOSBook/ch19.html#_view_controller_and_view_creation

and in this webcast:

http://www.youtube.com/watch?v=zIufcKpDIRo

matt
  • 515,959
  • 87
  • 875
  • 1,141