且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

StoryBoard 处理继承的视图控制器

更新时间:2023-02-12 12:30:39

你不能在故事板的子类中继承超类的布局,即如果你在故事板的视图控制器中直观地布置元素并连接它们要用于代码、视图控制器的子类,甚至同一类的其他实例,都必须单独布置,并且不会自动填充或更新.

You can't inherit the layout of a superclass in a subclass in a storyboard, i.e. if you visually lay out elements in a view controller in a storyboard and connect them to code, subclasses of that view controller, and even other instances of the same class, will have to be laid out individually, and will not automatically be populated or updated.

换句话说,在故事板中,您必须手动布置和连接所有界面元素在添加到故事板的每个单独实例和子类中.这为您提供了灵活性,因为您可以在整个应用中重复使用同一类的多个实例并以不同的方式对其进行布局,但您无法继承布局.

In other words, in a storyboard, you will have to manually lay out and connect all your interface elements in every individual instance and subclass that you add to the storyboard. This gives you flexibility in that you can reuse multiple instances of the same class throughout your app and lay them out differently, but it does not give you the ability to inherit layouts.

如果你想在子类中继承你的布局,在你的超类的viewDidLoad中以编程方式进行你的布局,然后你所有的子类都会有那些界面元素,即使你设计和布局它们在您的故事板中(它们在故事板中不可见,但会在您构建和运行您的应用时出现).

If you want to inherit your layout in subclasses, do your layout programmatically in the viewDidLoad of your superclass, and then all of your subclasses will have those interface elements, even if you design and lay them out in your storyboard (they will not be visible in the storyboard, but they will appear when you build and run your app).

基本上,如果您希望在一个类及其所有子类中具有相同的界面元素,请以编程方式创建它们,并且它们将存在于所有实例和子类实例中,即使您在您的故事板.

Basically, if you want to have interface elements that are the same in a class and all its subclasses, create them programmatically, and they will exist in all instances and subclass instances, even if you create and design the instances themselves in your storyboard.

您可以混合使用代码和故事板,因此您可以在故事板中创建一些元素,但其他需要在代码中出现在所有实例和子类中的元素.

You can mix code and storyboard, so you can create some elements in your storyboard, but others that need to be present in all instances and subclasses, in code.