将带有xib的UIView子类添加到UIViewController

时间:2010-10-30 04:40:16

标签: iphone uiview uiviewcontroller iphone-sdk-4.1

我在这里做错了什么?

创建:

CustomTab.h

#import <UIKit/UIKit.h>

@interface CustomTab : UIView {
 IBOutlet UIView *view;
}

@property (nonatomic, retain) IBOutlet UIView *view;

@end

CustomTab.m

#import "CustomTab.h"
@implementation CustomTab    
@synthesize view;

- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
        // Initialization code
    }
    return self;
}

- (void)dealloc {
    [super dealloc];
}
@end
  • XIB文件将其文件所有者设置为CustomTab的类,将视图连接起来

在我的UIViewController类

- (void)viewDidLoad {

   [super viewDidLoad];   

   CGRect frame = CGRectMake(0, 0, 320, 40); // Replacing with your dimensions 
   CustomTab *myObj = [[CustomTab alloc] initWithFrame:frame];

   [self.view addSubview:myObj.view];
   [myObj release];
}

子视图不会出现在屏幕上。我错过了什么?

1 个答案:

答案 0 :(得分:2)

nib文件不会自动将自身绑定到您的UIView。如果您的视图是所有者,我猜您可以在初始化视图后使用loadNibNamed:owner: NSBundle界面加载视图。