How to create a custom UIView?

advertisements

I've created a UIView subclass and corresponding xib file where I've laid out some UILabels and UIImageViews. I want to then put multiple copies of this custom UIView into a UIViewController.

When I do that they appear blank in interface builder and don't appear when the app loads. What methods do I need to implement on the UIView subclass to make this work?


The easiest method by far is to create a nib with File's Owner set to NSObject, containing one view containing your layout element.

Then

NSArray *arr = [[NSBundle mainBundle] loadNibNamed:@"MyView" owner:nil options:nil];
UIView *fv = [[arr objectAtIndex:0] retain];

// now you have the view, do something with it:
fv.center = CGPointMake(100,100);
[self.view addSubview:fv];

No need to do anything with the file's owner; it is even set to nil here.