如何使 .xib 显示在自定义类中
我想知道如何连接我的自定义类和我的 .xib。在 Interface Builder 中,我已将文件所有者的类更改为我的自定义类。然后,我创建了一个 IBOutlet 并将其连接到 .xib 的视图。然后我将自定义类的一个实例添加到 UIViewController 中。我的自定义类是 UIView 的子类,因此我将其背景设置为黑色,并且可以看到它出现在 UIViewController 的顶部。我看不到的是我的.xib???这是我的自定义类的代码...(FreeThrow 是我的自定义类...FetView 是我的 .xib)
@interface FreeThrow : UIView {
IBOutlet UIView *mySquareView;
}
@property(nonatomic, retain)IBOutlet UIView *mySquareView;
-(void)createMe;
@end
@implementation FreeThrow
@synthesize mySquareView;
-(void)createMe {
[self addSubview:mySquareView];
[self setBackgroundColor:[UIColor blackColor]]; // I did this to know my UIViewController is showing this class... which it is
}
@end
这是我从 UIViewController 调用的代码...
freeThrowView = [[FreeThrow alloc] init];
[freeThrowView createMe];
freeThrowView.frame = CGRectMake(8, 42, 335, 230);
[self addSubview:freeThrowView];
我需要添加什么代码?我需要做什么?问题是什么?为什么我的 .xib 没有显示。
I was wondering how to connect my custom class and my .xib. In Interface Builder I have changed the class of the Files Owner to my custom class. I then created an IBOutlet and connected it to the view of the .xib. I then added an instance of my custom class to my UIViewController. My custom class is a subclass of UIView, so I set the background of it to black and can see it appear on top of my UIViewController. What I can't see is my .xib???? Here is my code for my custom class... (FreeThrow is my custom class... FetView is my .xib)
@interface FreeThrow : UIView {
IBOutlet UIView *mySquareView;
}
@property(nonatomic, retain)IBOutlet UIView *mySquareView;
-(void)createMe;
@end
@implementation FreeThrow
@synthesize mySquareView;
-(void)createMe {
[self addSubview:mySquareView];
[self setBackgroundColor:[UIColor blackColor]]; // I did this to know my UIViewController is showing this class... which it is
}
@end
Here is my code for what I call from my UIViewController...
freeThrowView = [[FreeThrow alloc] init];
[freeThrowView createMe];
freeThrowView.frame = CGRectMake(8, 42, 335, 230);
[self addSubview:freeThrowView];
What code do I need to add? What do I need to do? What is the problem? Why is my .xib not showing up.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
遗憾的是,苹果并没有为我们提供一种仅通过 IB 真正将 xib 链接到视图的方法。您必须通过实例化来完成此操作。以下是类似问题的一些过去的答案:
如何是否将 nib (.xib) 文件与 UIView 关联?
将 .xibs 加载到 UIView
Sadly apple doesn't give us a way to truly link a xib to a view via IB alone. You'll have to do it via instantiation. Here are some past answers to similar questions:
How do I associate a nib (.xib) file with a UIView?
Loading .xibs into a UIView