从nib实例化视图会引发错误 [英] Instantiate view from nib throws error

查看:233
本文介绍了从nib实例化视图会引发错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在此之后创建@IBDesignable UIView子类(



有什么想法吗?谢谢

解决方案

你发布的 loadViewFromNib()方法看起来很好,你正确地获得了bundle并将nib名称指定为字符串文字,所以它应该找到nib。正如其他人评论的那样,你的笔尖设置可能有问题。以下是一些要检查的事项:


  1. 您应该将 AdvancedCellView 设置为身份检查器中的文件所有者自定义类。

  2. 检查模块是否正确 - 通常只需要它是空白的。

  3. 自定义类不应设置视图,只需将其保留为默认值(UIView)。如果你的侧边栏中的名称比查看更具描述性,那可能就错了。

  4. 检查笔尖中是否只有一个顶级对象。当您将视图树折叠时,侧边栏应如下所示,没有任何其他条目。



NB这个答案与OP链接的教程有关,而不是设置笔尖的唯一正确方法。


I tried to make @IBDesignable UIView subclass following this (link) tutorial. First custom view goes fine. But when I try to make another one, I have errors. First I got failed to update auto layout status: the agent crashed and Failed to render instance of .... Somehow I started to be able to biuld and run project with these errors, but then I get new error - EXC_BAD_ACCESS ... on line let view = nib.instantiateWithOwner(self, options: nil)[0] as! UIView. Here is whole method:

func loadViewFromNib() -> UIView {

        let bundle = NSBundle(forClass: self.dynamicType)
        let nib = UINib(nibName: "advancedCellView", bundle: bundle)
        let view = nib.instantiateWithOwner(self, options: nil)[0] as! UIView

        return view
    }

With first custom UIView is no problem. I use same code..

Any ideas? Thank you

解决方案

The loadViewFromNib() method you post looks fine, you're getting the bundle correctly and specifying the nib name as a string literal, so it should find the nib. As someone else commented, there's probably something wrong with your set up of the nib instead. Here are a few things to check:

  1. You should have AdvancedCellView set as the File's Owner Custom Class in the Identity Inspector.
  2. Check that the module is correct too - usually you just want it blank.
  3. The Custom Class for the View should not be set, just leave it as the default (UIView). If you have a more descriptive name than 'View' in the sidebar, it's probably wrong.
  4. Check that you only have one top level object in the nib. The sidebar should look like this, without any other entries, when you have the View tree collapsed.

N.B. This answer is related to the tutorial that the OP linked to, and not the only correct way to set up a nib.

这篇关于从nib实例化视图会引发错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆