Ohad Regev
Ohad Regev

Reputation: 5711

UIView with Separated .h, .m and xib files

I want to create a UIView that will appear in several different UIViewControllers upon user action. For example, a "How To" pop view that whenever the user clicks on a "?" button it pops holding the relevant information about the specific action the user is interested in. The view may also have other elements like a "done" button and a UITextView that will hold the text, etc...

Intuitively, it makes me think of creating a separated UIView with .h, .m and xib files and have each UIViewController that requires this UIView will simply alloc+init it and do [self.view addSubView:flexUIView] or insert it with animation. Well... it doesn't work...

I couldn't find any tutorial that explains how to do something like that.

Is anyone familiar with this approach and have some directions?

If not, what is the common approach for such a scenario?

Upvotes: 0

Views: 301

Answers (3)

Paul Solt
Paul Solt

Reputation: 8405

Here's a tutorial I wrote for creating a custom UIView with .xib, .h, .m files. I've added two sample projects showing Interface Builder and programmatic approaches.

https://github.com/PaulSolt/CompositeXib

Upvotes: 0

Ohad Regev
Ohad Regev

Reputation: 5711

I also found this tutorial that also has a link to the projects files and it gives a better idea regarding to how to do this:

Creating Reusable UIViews

Upvotes: 0

Afsal Meerankutty
Afsal Meerankutty

Reputation: 366

Alloc init will not load the nib of your custom UIView as in ViewControllers. You should load the nib using the below code after alloc init

Suppose you have CustomView.h, CustomView.m, CustomView.xib

        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomView" owner:self options:nil];
    yourView = (CustomView *)[nib objectAtIndex:0];

Upvotes: 1

Related Questions