You need to load it using the -loadNibNamed
method. -initWithNibName
is only for UIViewControllers.
Add the following code to your MyCustomView init method:
NSArray *subviewArray = [[NSBundle mainBundle] loadNibNamed:@"MyCustomView" owner:self options:nil]; UIView *mainView = [subviewArray objectAtIndex:0]; [self addSubview:mainView];
Remember, if you are initializing an object from a nib, it calls - (id)initWithCoder:(NSCoder *)aDecoder
to initialize, so you'll have to override that if you are creating the MyCustomView object within the nib. If you're just doing it with initWithFrame:
, then just override that and add the code above. Also, in your nib, make sure you have one top-level UIView, and place all other elements within that (that makes sure that your subviewArray only has one entry).
This will load the views from the nib and add them to the object, and should do the trick.
Conversation
------------------------------------------------------------------------------------------
I used your code in the initWithFrame and now I can see the MyCustomView with all its elements, but there is one thing that doesn't work. In the viewDidLoad of MyViewController I've: UIView *subView = [[MyCustomView alloc] initWithFrame:myTableView.frame]; [subView setBackgroundColor:[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.5]]; [myTableView addSubview:subView]; [subView release]; so that I should see the table under it. Any ideas? – Cricket
------------------------------------------------------------------------------------------
It works, if I set it in the initWithFrame. Now I've to understand how support the landscape mode for this view :) Thanks
------------------------------------------------------------------------------------------
I've found another solution that resolves also the landscape problem. I use the loadNibNamed code in MyViewController (when I have to display the subview) and I put any other customization in the awakeFromNib method in MyCustomView.m –
------------------------------------------------------------------------------------------
Hey, the method [self addSubview:mainView] only adds the view you made in IB to the MyCustomView. I tried your method and then tried to set the frame for MyCustomView. Turns out that the frame changes but the frame of the view made in .xib does not change. How do i change the frame of the IB view through MyCustomView? – HG's
------------------------------------------------------------------------------------------
Just do mainView.frame = newFrame. So, if you want the IB view's frame to take up the entire MyCustomView frame, do "mainView.frame = self.bounds". – Ned
------------------------------------------------------------------------------------------
相关推荐
The benefit of a column maker is that it can help you to format your text/code, or in some cases to make it easier to read in complex nested logic. Quick Open UltraEdit and UEStudio provide multiple ...
Chapter 3—Editors, IDE’s and Other tools.........................................................................28 3.1 Basic requirements for a decent Go development environment......................
Chapter 3—Editors, IDE’s and Other tools.........................................................................28 3.1 Basic requirements for a decent Go development environment......................
in TFlexControl.ControlCreate and all changes inside it fall into control creating undo-group. - FIX The current scale ignores in TFlexBox.GetRefreshRect and TFlexEllipse.GetRefreshRect methods ...
Available options (for this frontend in Zend_Cache factory) 4.3.6.3. Examples 4.4. Zend_Cache后端 4.4.1. Zend_Cache_Backend_File 4.4.2. Zend_Cache_Backend_Sqlite 4.4.3. Zend_Cache_Backend_...