使用 XCode 故事板实例化使用 XIB 进行设计的视图控制器

发布于 2025-01-02 23:04:30 字数 212 浏览 4 评论 0原文

我之前创建了一个 UIViewController 子类,它的控件布局在 XIB 文件中。

我现在想在故事板中使用这个视图控制器,但似乎虽然我可以在故事板中指示视图控制器的类名,但我无法告诉它加载 XIB。

我不想将 XIB 中的所有内容移至故事板,而是将其保留在单独的 XIB 中。

如何在故事板中获取此 UIViewController 来加载我的 XIB?

I have a UIViewController subclass that I created previously that had its controls laid out in a XIB file.

I'd like to use this view controller in a storyboard now, but it seems although I can indicate a view controller's classname in the storyboard, I can't tell it to load the XIB.

I'd rather not move everything from the XIB to the storyboard but keep it in its separate XIB.

How can I get this UIViewController in the storyboard to load my XIB?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谁的新欢旧爱 2025-01-09 23:04:30
  1. 删除故事板中视图控制器包含的视图。

  2. 然后通过配置同名的 nib 文件来提供视图
    作为视图控制器类。
    例如,如果视图控制器
    类名为 MyViewController,将您的 xib 文件命名为
    MyViewController.xib。

编辑 请注意,Swift Seed 5 开始破坏此技术,因为它破坏了它正在查找的 .xib 文件的名称。请参阅我的答案以获取解决方法: https://stackoverflow.com/a/25539016/341994 基本上这个名称匹配是<在 iOS 8 中出现了问题,但后来 Apple 后悔并修复了它,以便在 iOS 9 中再次正常工作。

  1. Delete the View contained by the view controller in the storyboard.

  2. Then provide the view by configuring a nib file with the same name
    as the view controller class
    . For example if the view controller
    class is called MyViewController, name your xib file
    MyViewController.xib.

EDIT Note that Swift seed 5 started breaking this technique, because it mangles the name of the .xib file it's looking for. See my answer here for workarounds: https://stackoverflow.com/a/25539016/341994 Basically this name matching was broken in iOS 8, but then Apple repented and fixed it so that it works again in iOS 9.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文