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
------------------------------------------------------------------------------------------
相关推荐
在IT行业中,二维码(Quick Response Code)已经成为数据传输和信息展示的重要工具,广泛应用于网站链接、产品信息、电子票务等多个领域。JavaScript是一种轻量级的脚本语言,常用于网页交互和动态效果的实现。在...
《Apress.Pro.dot.NET.2.0.Windows.Forms.and.Custom.Controls.in.C.Sharp.Dec.2005.pdf》这本书深入探讨了使用C#语言在.NET Framework 2.0环境下开发Windows Forms应用程序以及自定义控件的核心技术。这本书是针对...
jquery.mobile.custom.min.js v1.4.2 偶然发现这文件不好找还有挺贵,所以传一个
[confluence插件] custom-charts-server-5.2.12.obr [confluence插件] custom-charts-server-5.2.12.obr [confluence插件] custom-charts-server-5.2.12.obr [confluence插件] custom-charts-server-5.2.12.obr ...
A UIKit custom transition that simulates an elastic drag. Written in Swift..zip,模拟弹性阻力的uikit自定义转换。用斯威夫特写的。
Hex.Workshop.Professional.6.8.0.5419.incl.Patch.And.Custom-MPT
You will start by creating your first Android custom view and go through the design considerations. You will then see how the right choices will enable your custom view to perform seamlessly across ...
1.4. What’s new in Spring 1.4.1. What was new in Spring 3.1? 1.4.2. What was new in Spring 3.2? 1.4.3. What’s new in Spring 4.0? 1.5. Summary Chapter 2. Wiring beans 2.1. Exploring Spring’s ...
It shows you how to change and manage the general setting of your store and manage the modules and their layout, along with files and folders of the default theme. You will explore the featured module...
CoCServer-1-v15-U2 (Custom & CoC Mods - Unlimited).apk
EO.WebBrowser is a web browser engine based on Google's Chromium project but with native .NET programming interface --- don't worry, it's not a wrapper around the Chrome browser installed on your ...
It shows you how to change and manage the general setting of your store and manage the modules and their layout, along with files and folders of the default theme. You will explore the featured module...
For instance, the code `$("div.elements").addClass("myClass").load("ajax_url").fadeIn()` demonstrates how easily one can manipulate elements, load content asynchronously, and apply animations. ...
Custom.mxtpro
15 Adding Functionality to Your Custom Device ................................................................. 16 Custom Device Code Structure ..........................................................
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 ...