【问题标题】:Adding a UIView subclass with xib to UIViewController将带有 xib 的 UIView 子类添加到 UIViewController
【发布时间】:2010-10-30 04:40:15
【问题描述】:

我在这里做错了什么?

创建:

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

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

【问题讨论】:

  • 您希望在 IB 中出现什么?
  • 你是怎么检测到视图没有出现的,给视图赋予不同的颜色并检查
  • 什么意思?我希望在应用加载时在我的 UIViewController 子类上看到我的自定义 UIView
  • @Warrior UIViewController 子类是一种颜色,我的 UIView 子类在 XIB 中是另一种颜色,应用启动时我只看到一种颜色(UIViewControllers)
  • 您是否尝试过以编程方式代替 IB

标签: iphone uiview uiviewcontroller iphone-sdk-4.1


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2011-06-15
    • 2012-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-29
    • 2018-10-07
    相关资源
    最近更新 更多