使用 xib 添加 UIView 子类到 UIViewController

发布于 2024-09-30 01:33:21 字数 927 浏览 2 评论 0原文

我在这里做错了什么?

创建:

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];
}

中子视图不会出现在屏幕上。我缺少什么?

What am I doing wrong here?

Created:

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 file set its files owner to be CustomTab's class, hooked up the view

In my UIViewController class

- (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];
}

The subview doesnt appear on the screen. What am I missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

原谅过去的我 2024-10-07 01:33:21

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

The nib file does not bind itself to your UIView automatically. If your view is owner, I guess you can use loadNibNamed:owner: of NSBundle interface to load your view after your have init your view.

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