我有一个UIView的子类“s”。我想在s上放一些按钮和标签。我如何将我的UIView子类与一个nib文件关联起来?
我应该如何将Nib(.xib)文件与UIView相关联?
原标题:
最佳回答
- In Interface Builder, create a new
xib with the View template.
- Click on the view in the list of objects in the xib (you should also see "File s Owner and "First Responder").
- Push Cmd-4 to open the Identity pane of the inspector.
- Type your class s name into the "Class Name" field and push return.
您应该能够拖动按钮来收起它们。要从代码中访问符尖方式,请使用- [NSBundle loadNibNamed:owner:options:]
。您的视图应该是返回的数组中的第一个对象。
问题回答
在我的情况下,我不希望我的视图控制器了解我的视图.xib的IBOutlets。我想让我的视图子类拥有IBOutlets。不幸的是,UIView没有一个initWithNibName:
方法,所以我只创建了自己的类别。
这是我做的:
- In IB, click on your main UIView, and in the Identity Inspector, set the class to your subclass
- In IB, click on File s Owner, and in the Identity Inspector, set the class to your subclass
- Use your new category method
initWithNibName:
to instantiate your view.
这是我创建的分类:
- (instancetype)initWithNibName:(NSString *)nibName
{
NSArray *arrayOfViews = [[NSBundle mainBundle] loadNibNamed:nibName owner:self options:nil];
if (arrayOfViews.count < 1) {
return nil;
}
self = arrayOfViews[0];
return self;
}
受到此帖子的启发。
Note though, that so far the frame will adjust automatically, so unlike the code in the post, I haven t yet had to explicitly set the frame.
Also, unlike the post s code, I needed to set owner:self
so the IBOutlets would be wired up correctly.
相关问题
热门标签
- winforms
- combobox
- fogbugz
- java
- date
- internationalization
- asp.net
- iis
- url-rewriting
- urlrewriter
- c#
- enums
- ocaml
- haxe
- algorithm
- string
- viewstate
- .net
- c++
- c
- symbol-table
- mysql
- database
- postgresql
- licensing
- migration
- vb.net
- vb6
- declaration
- vb6-migration
- python
- psycopg2
- backup
- vmware
- virtualization
- gnu-screen
- authentication
- desktop
- excel
- xll
- cultureinfo
- regioninfo
- oracle
- client
- session
- download
- html
- virtual
- constructor
- scenarios
- perl
- full-text-search
- javascript
- ajax
- testing
- oop
- inheritance
- vim
- encapsulation
- information-hiding