如何在自定义类中显示.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没有出现。     

解决方法

        遗憾的是,Apple并没有给我们提供一种仅通过IB真正将xib链接到视图的方法。您将必须通过实例化来完成。以下是一些类似问题的解答: 如何将笔尖(.xib)文件与UIView关联? 将.xibs加载到UIView中