如何为多个子类使用单个故事板 uiviewcontroller [英] How to use single storyboard uiviewcontroller for multiple subclass

查看:18
本文介绍了如何为多个子类使用单个故事板 uiviewcontroller的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个包含 UINavigationController 作为初始视图控制器的故事板.它的根视图控制器是UITableViewController的子类,即BasicViewController.它有 IBAction 连接到导航栏的右侧导航按钮

从那里我想使用故事板作为其他视图的模板,而不必创建额外的故事板.假设这些视图将具有完全相同的界面,但具有 SpecificViewController1SpecificViewController2 类的根视图控制器,它们是 BasicViewController 的子类.
除了 IBAction 方法之外,这两个视图控制器将具有相同的功能和界面.
应该是这样的:

Let say I have a storyboard that contains UINavigationController as initial view controller. Its root view controller is subclass of UITableViewController, which is BasicViewController. It has IBAction which is connected to right navigation button of the navigation bar

From there I would like to use the storyboard as a template for other views without having to create additional storyboards. Say these views will have exactly the same interface but with root view controller of class SpecificViewController1 and SpecificViewController2 which are subclasses of BasicViewController.
Those 2 view controllers would have the same functionality and interface except for the IBAction method.
It would be like the following:

@interface BasicViewController : UITableViewController

@interface SpecificViewController1 : BasicViewController

@interface SpecificViewController2 : BasicViewController

我可以这样做吗?
我可以只实例化 BasicViewController 的故事板,但让根视图控制器子类化 SpecificViewController1SpecificViewController2 吗?

谢谢.

Can I do something like that?
Can I just instantiate the storyboard of BasicViewController but have root view controller to subclass SpecificViewController1 and SpecificViewController2?

Thanks.

推荐答案

很好的问题 - 但不幸的是只有一个蹩脚的答案.我不相信目前可以执行您的建议,因为 UIStoryboard 中没有初始值设定项允许覆盖与故事板关联的视图控制器,如初始化时故事板中的对象详细信息中定义的那样.在初始化时,stoaryboard 中的所有 UI 元素都链接到它们在视图控制器中的属性.

great question - but unfortunately only a lame answer. I don't believe that it is currently possible to do what you propose because there are no initializers in UIStoryboard that allow overriding the view controller associated with the storyboard as defined in the object details in the storyboard on initialization. It's at initialization that all the UI elements in the stoaryboard are linked up to their properties in the view controller.

默认情况下,它将使用故事板定义中指定的视图控制器进行初始化.

It will by default initialize with the view controller that is specified in the storyboard definition.

如果你试图重用你在故事板中创建的 UI 元素,它们仍然必须链接或关联到视图控制器使用它们的属性,以便它们能够告诉"视图控制器有关事件的信息.

If you are trying to gain reuse of UI elements you created in the storyboard, they still must be linked or associated to properties in which ever view controller is using them for them to be able to "tell" the view controller about events.

复制故事板布局并不是什么大事,特别是如果您只需要 3 个视图的类似设计,但是如果您这样做,您必须确保清除所有先前的关联,否则会崩溃当它尝试与前一个视图控制器通信时.您将能够在日志输出中将它们识别为 KVO 错误消息.

It's not that much of a big deal copying over a storyboard layout especially if you only need a similar design for 3 views, however if you do, you must make sure that all the previous associations are cleared, or it will get crashes when it tries to communicate to the previous view controller. You will be able to recognize them as KVO error messages in the log output.

您可以采取的几种方法:

A couple of approaches you could take:

  • 将 UI 元素存储在 UIView 中 - 在一个 xib 文件中,并从您的基类实例化它,并将其添加为主视图中的子视图,通常是 self.view.然后,您可以简单地使用故事板布局,其中基本上空白的视图控制器在故事板中占据一席之地,但为它们分配了正确的视图控制器子类.由于他们将从基础继承,因此他们将获得该视图.

  • store the UI elements in a UIView - in a xib file and instantiate it from your base class and add it as a sub view in the main view, typically self.view. Then you would simply use the storyboard layout with basically blank view controllers holding their place in the storyboard but with the correct view controller sub class assigned to them. Since they would inherit from the base, they would get that view.

在代码中创建布局并从您的基本视图控制器安装它.显然,这种方法违背了使用故事板的目的,但在您的情况下可能是可行的方法.如果您有应用程序的其他部分可以从故事板方法中受益,则可以在适当的情况下在这里和那里偏离.在这种情况下,像上面一样,您只需使用分配了子类的银行视图控制器,并让基本视图控制器安装 UI.

create the layout in code and install it from your base view controller. Obviously this approach defeats the purpose of using the storyboard, but may be the way to go in your case. If you have other parts of the app that would benefit from the storyboard approach, it's ok to deviate here and there if appropriate. In this case, like above, you would just use bank view controllers with your subclass assigned and let the base view controller install the UI.

如果 Apple 想出一种方法来执行您的建议就好了,但是将图形元素与控制器子类预先链接的问题仍然是一个问题.

It would be nice if Apple came up with a way to do what you propose, but the issue of having the graphic elements pre-linked with the controller subclass would still be an issue.

新年快乐!!好好的

这篇关于如何为多个子类使用单个故事板 uiviewcontroller的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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