如何使 .xib 显示在自定义类中

发布于 2024-11-11 18:51:57 字数 954 浏览 5 评论 0原文

我想知道如何连接我的自定义类和我的 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

总攻大人 2024-11-18 18:51:57

遗憾的是,苹果并没有为我们提供一种仅通过 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文