Lud*_*uda 22 iphone objective-c uiview ios
我UIView用xib 创建了自定义.
另外我UIViewController在我的故事板中有一个,我添加了一个UIView并将其类设置为我的自定义UIView.
但是当我运行应用程序时,视图没有其子视图.调试时,所有子视图都为null.
在自定义的.m中UIView,存在以下init方法:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
}
return self;
}
Run Code Online (Sandbox Code Playgroud)
我错过了什么?
sta*_*Man 31
正如您所知,UIViewController我们有-initWithNibName:bundle:一种方法可以将它与xib连接起来.
但是......
当涉及到a时UIView,你需要使用-loadNibNamed:owner:options:xib 来加载它.(只是在xib中为视图指定自定义类将不起作用)
UIView名为的子类CustomXIBView
UIView)CustomXIBView
CustomXIBView笔尖View(左侧工具栏)Show Identity Inspector(右侧面板中的第3个选项)CustomXIBView作为自定义类的View
CustomXIBView的File's Owner在笔尖CustomXIBView.h//To load `CustomXIBView` from any `UIViewController` or other class:
//instead of the following commented code, do the uncommented code
//CustomXIBView *myCustomXIBViewObj = [CustomXIBView alloc] init];
//[myCustomXIBViewObj setFrame:CGRectMake(0,0,320,480)];
//Do this:
CustomXIBView *myCustomXIBViewObj =
[[[NSBundle mainBundle] loadNibNamed:@"someView"
owner:self
options:nil]
objectAtIndex:0];
[myCustomXIBViewObj setFrame:CGRect(0,
0,
myCustomXIBViewObj.frame.size.width,
myCustomXIBViewObj.frame.size.height)];
[self.view addSubview:myCustomXIBViewObj];
Run Code Online (Sandbox Code Playgroud)
参考:http://eppz.eu/blog/uiview-from-xib/
| 归档时间: |
|
| 查看次数: |
23534 次 |
| 最近记录: |