Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

StoryBoard handling inherited view controller

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?

like image 275
TedCheng Avatar asked Dec 21 '12 04:12

TedCheng


2 Answers

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.

like image 90
Greg Avatar answered Oct 17 '22 05:10

Greg


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'.

like image 39
Wasenhorn Avatar answered Oct 17 '22 05:10

Wasenhorn