0

Sorry if this has an obvious answer, I can't seem to find anything describing this situation. How can I set up a nib file which directly implements a custom UIView? In other words, the top-level view in the nib file becomes an instance of my custom UIView class? The closest I've been able to find so far is to create a custom UIView which loads the nib and sets it's top-level view to a UIView* property (and all it's subviews to the appropriate outlets by loading with owner:self). This works, but as I understand the top-level View does nothing except act as a container for all the other views - which is what my custom UIView is supposed to do. Any suggestions?

Edit: I got this in the end: the issue is my custom view class was set as the File's Owner class, when it should have been set as the top-level View's class. I didn't know it was possible to set outlets on elements in the IB as well as the Owner. Setting the Owner to the class of the ViewController that will hold it and the top-level view to my custom view has it all working.

2 Answers 2

0

If you create a project using the single view template, you will get an empty view. If you add a class that is a subclass of UIView, you can change the class of that view in IB to your class (in the identity inspector).

Sign up to request clarification or add additional context in comments.

3 Comments

This sounds rather like it's addressing using UIViewControllers for nibs, rather than UIViews - I'm fine with both view controllers and the use of custom classes in Interface Builder, I'm just having an issue here with accessing a top-level UIView in a loaded nib without using an additional, seemingly unnecessary UIView in the middle.
I don't understand your first sentence at all. If you do what I posted, the top level view, in fact the only view, will be an instance of your class -- that's what you said you wanted in your comment to Srikar's post. You can then add any other subview to your custom view that you want.
All fixed as described in the question edit: I was setting File's Owner to my custom class, not the top-level view. Listed this as correct answer as the steps do actually make it clear to set the class of the view, I just didn't read thoroughly enough :)
0

If I have understood your question correctly, then this should help -

In the IB, on the right-pane. select the "custom class" dropdown & fill in your custom UIView class. The image shows UITableViewCell instead of that put your custom UIView class name.

enter image description here

Correct me if I have not understood your question correctly.

3 Comments

This doesn't crash, but it doesn't display as the top-level view isn't accessed in any way (and thus none of it's child views are either). What I'm trying to find is a way to make the top-level view the actual instance of my custom class (if possible); rather than setting it as an outlet or adding it as a subview.
even if you are using a custom class, since you are using an IB you need to set IBOutlet... otherwise how else will the linkage happen?
What I have at the moment (where -> is a parent/child relationship), is MyCustomView->Top-level UIView in nib->all the views in the nib. What I'm aiming for is MyCustomView->all the views in the nib, so the top-level view is the custom class, rather than being contained within it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.