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

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

问题描述

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



从那里我想使用故事板作为其他视图的模板,而无需创建额外的故事板。假设这些视图具有完全相同的接口,但具有类 SpecificViewController1 SpecificViewController2 的根视图控制器, code> 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 的故事板,但有根视图控制器子类 SpecificViewController1 SpecificViewController2



谢谢。

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.

如果苹果想出一个方法来做你想要的,但是让图形元素与控制器子类预链接的问题仍然是一个问题,这将是很好的。

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.

有一个伟大的新年!
很好

have a great New Year!! be well

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

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