【问题标题】:Why does my view class not show up in storyboard?为什么我的视图类没有出现在情节提要中?
【发布时间】:2013-02-16 15:25:17
【问题描述】:

我添加了一个带有自己的 xib 的自定义视图类。在 xib 中,File's Owner -> Custom Class 设置为“MyCustomView”。

我在情节提要中添加了一个空白视图,其类设置为“MyCustomView”

当我运行我的应用程序时,我没有看到我的视图正确显示。但是,我确实看到了2013-03-01 16:52:48.283 Test[56785:c07] MyCustomView initWithCoder

任何想法出了什么问题?

我在这里隔离了这个错误:http://bit.ly/YdDbqU

谢谢!

编辑:

  1. 我关闭了自动布局
  2. 我使用了以下内容:

    UIView *subview = [[NSBundle mainBundle] loadNibNamed:@"MyCustomView" owner:self options:nil].lastObject; subview.frame = self.myCustomViewInstance.bounds; [self.view addSubview:subview];

  3. 一切正常!

【问题讨论】:

  • “正确显示”是什么意思。我看到一个空白的、白色的 UIView,应该是这样的
  • @CodaFi 以及 xib 有几个标签:1、2、3、4,...我指的是显示在主视图的空白处的 xib

标签: iphone ios objective-c cocoa


【解决方案1】:

你永远不会真正加载那个 xib——在 IB 中设置类是不够的。您仍然需要加载 xib 文件,并将控制器的视图设置为该视图;

#import "FirstViewController.h"
#import "MyCustomView.h"

@implementation FirstViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view = [[NSBundle mainBundle] loadNibNamed:@"MyCustomView" owner:self options:nil].lastObject;
} 

【讨论】:

  • 成功了!但是,设置 self.view 不是意味着替换了整个 FirstViewController 的视图(包括它的子视图 MyCustomView)吗?我在底部的 FirstMainViewController 中添加了一个额外的标签,现在标签不再可见,因为整个视图都被替换了。 (我创建了一个新的提交“Added label but label now missing?”,你介意再看一下吗?)
  • @iaman00b,如果您不想替换整个视图,请将该视图添加为 self.view 的子视图。
【解决方案2】:
  1. 我关闭了自动布局
  2. 我使用了以下内容:

    UIView *subview = [[NSBundle mainBundle] loadNibNamed:@"MyCustomView" owner:self options:nil].lastObject; subview.frame = self.myCustomViewInstance.bounds; [self.view addSubview:subview];

一切正常!

【讨论】:

    【解决方案3】:

    确保您的类是从 UIViewController 继承的。

    @interface ClassName : UIViewController
    

    【讨论】:

      猜你喜欢
      • 2021-09-01
      • 2011-09-08
      • 1970-01-01
      • 2016-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多