1

In storyboard I'm trying to do let views/controllers become parents/children from each other, although I don't know if this is possible.

The view controllers can have child view controllers, so I want to enforce the same structure on the views. Is this possible?

For example, say there is ParentViewController and ChildViewController (which are UIViewControllers). And then there's the corresponding ParentView and ChildView (which are UIView).

I specify a UIImageView IBOutlet *imageView in ParentView (and ParentViewController) right in the center, with some dimensions (e.g. the view is 400 by 400).

What I do know is that ChildViewController is able to access the property in the ParentViewController. But can I also make ChildView connect to that outlet by some kind of mechanism from ParentView? There doesn't seem to be a "dragging mechanism" in Storyboard.

I looked into questions like these, but they do not have the answer, because it's partially outdated (and partially different).

Community
  • 1
  • 1
Melvin Roest
  • 1,240
  • 1
  • 12
  • 27
  • 1
    Melvin, to help other people googling here, I'm going to edit your question a little. You know what it is: you're not actually asking about "inheritance". Inheritance is just when you **subclass** a class. (Example, there's UIButton, you make "UISpecialButton".) Here, you're asking about how to "connect" different views and/or controllers. You're talking about parent-child relationships (totally unrelated to subclassing). (Indeed note all the classes you discuss, are the same.) So I'm going to go ahead and edit, for future readers. You definitely undo or modify my edits as you see fit!! – Fattie May 24 '14 at 06:55
  • Incidentally if you're a game engine engineer (unity, unreal, etc), this is a common confusion. Game engines run entirely on a "parent-child" relationship. {*In fact, nothing more than a field pointing to 'parent' or 'child'.*} There are two problems, (A) new programmers in the field sometimes assume this has some relationship to subclassing (aka inheritance), it does not in any way; and (B) the language can get confused because someone might say something in general about "inheriting" some information or whatever, which is clear enough, but has no connection to "OO inheritance (subclassing)". – Fattie May 24 '14 at 07:02

1 Answers1

3

Melvin, it's possible this ...

https://stackoverflow.com/a/23403979/294884

is what you're after. Everything's a container now ...

note, at the bottom there I mention "in passing" the two methods for getting at child the view controllers, it's like "the most important thing in modern iOS programming!" heh. (Staggeringly badly explained in the iOS docs.)

enter image description here .

To repeat, it's totally bizarre that Apple sort of "never mentions this" - it's pretty much the core concept in making anything other than the simplest apps.

Everything is amazingly easy in this new container-driven world (once they are hooked together!)

Hope I understood your question, cheers!

Community
  • 1
  • 1
Fattie
  • 30,632
  • 54
  • 336
  • 607
  • 1
    Don't get me wrong I like your answer that you have provided for the other question, but shouldn't this really be more of a comment as all you are doing is pointing them to your other answer. – Popeye May 18 '14 at 15:10
  • The OP's question is about inheritance, and doesn't appear to be related to container view controllers. – jlehr May 18 '14 at 15:15
  • Ah I get it! So container views are akin to frames in HTML. You got ParentViewController (PVC) and you hook it up to the ParentView (PV). You hook up the ChildVC to the container view in the PVC. Yep in that way you could simulate inheritance. But what if you need multiple childs on the same spot (while hiding the others)? I think it is possible to lay out multiple container views in the PVC, and only show one of them when needed. In that way you can implement multiple inheritance via IB. It feels a bit tricky / hackish, definitely a small paradigm shift for me. Thanks for the answer! – Melvin Roest May 19 '14 at 00:11
  • With regards to getting multiple view controllers in one container view, I found a tutorial that answers that question as well. http://sandmoose.com/post/35714028270/storyboards-with-custom-container-view-controllers – Melvin Roest May 19 '14 at 01:37
  • There is nothing wrong with phrasing a duplicate, provided this is actually a duplicate. An answer pointing to another question _yet tying it to the specifics of this one_ is actually helpful. – Tim Post May 21 '14 at 05:22
  • 1
    I wrote a comment to (potentially) clarify things for future readers. And I agree, I am a bit late with ticking it O:) – Melvin Roest May 23 '14 at 15:08
  • "But what if you need multiple childs on the same spot?" (while hiding the others)? **absolutely no problem!** Just did a job exactly like that. You simply swap them out. It's true that the containers "sit on each other" in storyboard, but that's always been an issue when you "swap things in and out" - it's not really a problem as it's just "a box" .. the content is in the actual controller off elsewhere on the page. – Fattie May 23 '14 at 15:42
  • Sometimes when you do that, it's like you're annoyingly making your own whole view controller system. But for me, often it's the best way. Just have one routine to turn them all off, and turn on the one you're interested in. – Fattie May 23 '14 at 15:43
  • Hey @Mikael ... I give away a huge number of points on bounties. Check out my history! (BTW all the answers I bounty are fantastic. When I'm reading around, if there's a great answer I give it 500 points with a bounty.) It's the new, cool, thing to do .. give away your points on bounties. – Fattie May 23 '14 at 15:45
  • Joe, yeah thats great! I've used bounties several times myself. Don't know what I've done without SO :) – Mikael May 24 '14 at 09:59