【问题标题】:Custom xib not loading in other Xib view controller自定义 xib 未加载到其他 Xib 视图控制器中
【发布时间】:2016-02-01 19:21:42
【问题描述】:

我有以下文件:

  • ViewController.xib
  • ViewController.h
  • ViewController.m
  • BannerView.xib
  • BannerView.h(继承自 UIView)
  • BannerView.m

我的视图控制器已连接并且工作正常。我创建了 BannerView 并按照本教程中的说明执行了必要的链接:http://www.maytro.com/2014/04/27/building-reusable-views-with-interface-builder-and-auto-layout.html

但是,在我的 ViewController.xib 中,当我添加 UIView 并将其类设置为 BannerView 时,它不会加载。该应用程序不会崩溃或发生任何事情。

我是否遗漏了任何步骤,我们将不胜感激。

编辑 1 - 添加图像和代码

BannerView.h
  @property (nonatomic, strong) IBOutlet UIView *view;

#import "BannerView.h"

@implementation BannerView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
   // Drawing code
}
*/

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if(self) {
        [self setup];
    }
    return self;
 }

- (void)setup {
    [[NSBundle mainBundle] loadNibNamed:@"BannerView" owner:self options:nil];
    [self addSubview:self.view];
}

@end

横幅视图 XIB - 文件所有者

横幅视图 - 视图

【问题讨论】:

  • 您能否显示“添加 UIView 并将其类设置为它只是不加载的 BannerView”的代码?

标签: ios objective-c interface-builder xib


【解决方案1】:

UIView 的子类不能有自己的 XIB 文件。请注意,当您创建 UIView 的子类时,包含 XIB 复选框未启用。如果您需要在 UIView 重载 drawRect 中进行自定义绘图。如果您只想将通用 UIKit 子视图添加到视图中,请在 UIViewController 的 XIB 中执行此操作,UIView 将出现在其中或以编程方式执行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-05
    • 1970-01-01
    • 1970-01-01
    • 2015-09-18
    • 2016-02-10
    • 2020-05-16
    • 1970-01-01
    • 2012-05-31
    相关资源
    最近更新 更多