加载 xib (nib) 文件两次以获得视图的两个不同实例

发布于 2024-09-11 00:22:20 字数 985 浏览 3 评论 0原文

我有一个 UIViewController* compViewController ,它可以在同一视图的两个副本之间切换(两个视图之间存在微小差异)。 目前,这两个视图都来自同一个 xib 文件。 xib 文件包含同一视图层次结构的两个几乎相同的副本。其中一个副本(“主副本”)连接到 compViewController 中的视图出口。 第二个视图连接到 compViewController 中的 auxView 出口。视图控制器选择显示一个或另一个视图。 事实上,这种描述对实际情况过于简单化了。实际上,两个视图之一作为“overlayView”传递到 UIImagePickerController,该 UIImagePickerController 由 compViewController 作为模态控制器推送。

事实证明,这种方法是维护的噩梦,因为对一个视图的任何更改都需要复制到笔尖中的另一个视图上。

我认为一种可能的解决方案是从笔尖中删除视图层次结构的一份副本,然后加载笔尖两次以获得视图的两份副本。然后,在将其作为叠加层传递给 UIImagePickerController 之前,在 compViewController 的 viewDidLoad 方法中以编程方式对其中之一进行必要的更改。

我在 NSBundle 的 UIKit 扩展类别中使用 loadNibNamed 方法(我认为这是一个仅限 iPhone 的类别),它需要一个所有者。如果所有者是同一个 ViewController,则生成的对象是同一个对象,即,为具有相同所有者的 sam xib 调用 loadNibNamed 两次将返回完全相同的实例。我相信我需要两个单独的实例,就像过去我尝试将 compViewController 使用的视图的相同实例传递给 ImagePicker 一样,但发生了各种不好的事情。然后我读到,你永远不应该在不同的 UIViewContollers 之间共享 UI 对象,至少不应该共享都推入同一个 NavigationController 的 ViewController。

我是否缺少更好的方法来做到这一点?

预先非常感谢您为我提供的任何建议。

I have a UIViewController* compViewController which switches between two copies of the same view (with small differences between the two views).
Currently, both views come form the same xib file. The xib file contains two almost identical copies of the same view hierarchy. One of the copies, "the main one" is connected to the view outlet in the compViewController.
The second view is connected to an auxView outlet in the compViewController. The view controller chooses to display one or the other view.
Actually, this description is an oversimplification of the actual situation. In reality one of the two views is passed as the "overlayView" to a UIImagePickerController which is pushed as a modal controller by the compViewController.

This approach turns out to be a maintenance nightmare because any changes to one view need to be replicated on the other view in the nib.

One possible solution I'm thinking would be to remove one copy of the view hierarchy from the nib and then load the nib twice to obtain two copies of the view. Then make the necessary changes programmatically to one of them in the compViewController's viewDidLoad method before it gets passed as the overlay to the UIImagePickerController.

I'm using the loadNibNamed method in the UIKit extension category for NSBundle (I think this is an iPhone only category) which takes an owner. If the owner is the same ViewController then the resulting object is the same object, i.e. calling loadNibNamed twice for the sam xib with the same owner returns the same exact instance. I believe I need two separate instances, as in the past I tried passing the same instance of the view used by the compViewController to the ImagePicker and all kinds of bad things happened. I read then that you should never share UI objects between different UIViewContollers, at least not to ViewControllers that are both pushed into the same NavigationController.

Am I missing a better way of doing this?

Thank you very much in advance for any advice you might have for me.

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

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

发布评论

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

评论(1

若能看破又如何 2024-09-18 00:22:20

我的理解是笔尖由 viewController 拥有,并且只能有一个所有者。我放弃了 nib,开始用代码构建一切。一开始需要做更多的工作,但由于我发现笔尖有限制,所以它更可重用且更强大。

My understanding was a nib was owned by a viewController and could only have one owner. I moved away from nibs and and started building everything in code. It takes a bit more work at the beginning but was more reusable and powerful as I was finding nibs limiting.

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