简体   繁体   English

在故事板上使用Swift嵌套类作为UIViewController

[英]Use Swift nested class as UIViewController in storyboard

Do you guys know if it's possible to use a Swift nested class as UIViewController in Storyboard? 你们知道在Storyboard中是否可以使用Swift嵌套类作为UIViewController吗?

I'm looking into different ways to structure my projects, one of which is grouping related classes by nesting them in the same main class. 我正在研究构建项目的不同方法,其中一种是通过将相关类嵌套在同一主类中来对它们进行分组。

Let me explain with an example: 让我用一个例子来解释:

Say I have a screen for displaying my app's settings which needs a UIViewController and a model. 假设我有一个屏幕来显示我的应用程序设置,这需要一个UIViewController和一个模型。
I could create 2 classes: SettingsController and SettingsModel , but I'm wondering if it wouldn't be nice to have it structured otherwise by having a main class Settings containing nested classes Controller and Model like so 我可以创建2个类: SettingsControllerSettingsModel ,但是我想知道是否通过将主类Settings包含嵌套类ControllerModel这样的主类来进行结构化不是很好吗?

class Settings {
   class Controller: UIViewController {
      ...
   }
   class Model {
      ...
   }
}

That way I could use them by doing Settings.Controller and Settings.Model which I think would look pretty nice. 这样,我可以通过执行Settings.ControllerSettings.Model来使用它们,我认为它们看起来非常不错。

Now this works fine expect when using storyboard. 现在,在使用情节提要时,效果很好。 In Storyboard when I select a view controller to set the custom class, if I type in Setting.Controller and hit enter, the field does not validate and is cleared. 在情节提要中,当我选择一个视图控制器来设置自定义类时,如果键入Setting.Controller并按Enter,则该字段将无效并被清除。

So my question is do you guys know what I'm doing wrong or if this is simply not possible? 所以我的问题是,你们是否知道我在做错什么,或者根本不可能吗?

Of course, it's after having posted the question that I found a solution so I'm sharing it for posterity. 当然,在发布问题后,我找到了解决方案,因此我将其分享给后代。

So there was 1 problem to the way I was trying to do it: 因此,我尝试执行此操作时遇到了一个问题:

  1. Nested classes are references by using the dot notation: Settings.Controller but Interface Builder does not see that as a valid class name 嵌套类是通过使用点符号表示的: Settings.Controller但是Interface Builder不会将其视为有效的类名。

The solution was simple, give the controller it's own Objc name: 解决方案很简单,为控制器指定自己的Objc名称:

class Settings {
   @objc(SettingsController)
   class Controller: UIViewController {
   }
   ...
}

By doing this you give the nested controller an ObjC name thereby exposing it the Interface Builder. 通过这样做,您可以为嵌套控制器指定一个ObjC名称,从而将其公开给Interface Builder。 Now you can reference the controller by filling in SettingsController . 现在,您可以通过填写SettingsController来引用控制器。

I don't think storyboards supports that (yet), so in the meantime you need to use some workarounds. 我认为故事板不支持(但),因此与此同时,您需要使用一些解决方法。

If all you want is the syntax Settings.Controller , you can do this: (Inspired by this answer ) 如果只需要语法Settings.Controller ,则可以执行以下操作:(受此答案启发)

Declare your Settings.Controller class not as a nested class, but as a standalone class SettingsController : 声明您的Settings.Controller类不是一个嵌套类,而是一个独立的类SettingsController

class SettingsController : UIViewController { ... }

Then in Settings , add a typealias : 然后在Settings中添加typealias

class Settings {
    typelias Controller = SettingsController
    class Model { ... }
}

Now you can use SettingsController in the storyboard, but Settings.Controller in code. 现在你可以使用SettingsController在故事板,但Settings.Controller代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM