【问题标题】:Ios: load custom view in xibios:在xib中加载自定义视图
【发布时间】:2015-09-18 07:10:42
【问题描述】:

我在 xib 中设计了我的自定义视图。我有CustomView.h/m + CustomView.xib。在 xib 文件中,我将视图类设置为 CustomView。

现在我想在其他 xib 中使用这个视图。我创建了另一个 xib,添加了视图并将其设置为 CustomView,但它没有加载。

如何正确加载我的 CustomView?

【问题讨论】:

  • 阅读这篇文章会对你有所帮助stackoverflow.com/questions/5095359/…
  • @Omarj 提供的答案提供了无限循环,您不能在 initWithCode 方法中调用 loadNib
  • 更准确地说,您如何加载CustomView 以及究竟发生了什么?你在推一个新的UIViewController吗?
  • @LancelotdelaMare 目前,我刚刚在其他 xib 视图中添加并将其类设置为 CustomView。没有任何反应,customview 没有加载。我想我需要写一些代码让它出现在 UI 中
  • 如果是这样,目前的问题是stackoverflow.com/questions/15405916/…的副本

标签: ios interface-builder


【解决方案1】:

简短回答:是的。

您可以在任意数量的.XIB 文件中使用CustomView.h/m,包括.storyboard

问题在于如何加载第二个视图。

    if let navigationController = self.navigationController {
        let sb = UIStoryboard(name: "Main", bundle: nil)
        if let vc = sb.instantiateViewControllerWithIdentifier("id") as? UIViewController {
            navigationController.pushViewController(vc, animated: true)
        }
    }

见:

【讨论】:

  • 我在问:如何加载它:)
  • 在那个问题视图中是从代码加载的,而不是从 xib/storyboard
【解决方案2】:

您需要使用IB_Designable 关键字来使您的自定义视图在IB 中看起来正确。但是在 nib 中设计自定义视图而不是覆盖 drawRect 会使它更加复杂。查看this link 了解如何操作:

【讨论】:

    猜你喜欢
    • 2017-02-05
    • 1970-01-01
    • 2016-02-01
    • 1970-01-01
    • 2013-04-07
    • 2014-02-04
    • 2013-08-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多