StoryBoard handling inherited view controller

♀尐吖头ヾ 提交于 2020-07-04 10:10:03

问题


I am new to storyboard. I used to make my view using code. I have a question.

I created a view controller

@interface FunctionViewController : UIViewController

And I have use my code to add

  1. a full screen button (alpha : 0.5)
  2. a popup-like view in the middle (not full screen)

Then I created another Viewcontroller

@interface PlayFunctionViewController : FunctionViewController

and add some views on the popup-like view

I want to recreate these stuff by using storyboard.

How can I build these using interface builder and storyboard so that I don't need to layout the full screen button and the popup-like view in every subclass of FunctionViewController?


回答1:


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.

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.




回答2:


I ran into a similar issue, and though it was time consuming but creating a delegate and reusing it in multiple view controllers was a much better solution. Although that beats the whole point of 'Inheritance'.



来源:https://stackoverflow.com/questions/13984475/storyboard-handling-inherited-view-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!