0

I'm trying to load a subclass of a UIViewController with its views defined in a xib file into a storyboard. Let's call it a NibViewController.

The point of this approach is to reuse the same ViewController in multiple screens of the app.

I know it's possible to do it manually in the code, but I'm looking for a solution in the storyboard. I've tried suggestions from other topics like this one, but nothing worked. The ViewController is correctly displayed in the simulator but not in the storyboard. Here is the code: https://github.com/srstanic/NibViewControllerInStoryboard and here is the screenshot:

enter image description here

Am I mistaken to expect the contents of the NibViewController to appear in the storyboard?

matt
  • 515,959
  • 87
  • 875
  • 1,141
Srđan Stanić
  • 740
  • 1
  • 8
  • 17

1 Answers1

0

Am I mistaken to expect the contents of the NibViewController to appear in the storyboard?

Yes, you are mistaken. Your app is working perfectly so you should stop worrying and just proceed.

By deleting the view from the view controller in the storyboard, you have specifically instructed the storyboard: "Do not make a view for this view controller. At runtime, the view should come from the xib file, not from you."

And that is exactly what does happen at runtime. So just design your interface in the xib file and all will be well.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Thank you for the clarification! This wasn't what I was hoping to learn, though :) My intention was to design a container controller in the xib file and then use it in the storyboard just as I would use UINavigationController. But if views of the container controller will not load in the storyboard, there is no way to add child view controllers to that container controller. Am I missing something here? – Srđan Stanić Nov 10 '17 at 22:17
  • "My intention was to design a container controller in the xib file and then use it in the storyboard just as I would use UINavigationController. But if views of the container controller will not load in the storyboard, there is no way to add child view controllers to that container controller." Well, sure, there's no content view in a _xib_. But you can add child view controllers in code, no problem. We were doing that before storyboards even existed: http://www.apeth.com/iOSBook/ch19.html#_container_view_controllers – matt Nov 10 '17 at 22:40
  • I was hoping there is a way to do it in the visual format. It's easier to grasp a complex view controller hierarchy from the storyboard than from the code. Thank you for your answers, they are much appreciated! – Srđan Stanić Nov 10 '17 at 22:47
  • Well, it seems to me you have not laid out your goals clearly enough. This seems to be an x-y problem. Instead of explaining what you really want to do, you have deleted the view controller's main view and now you are having remorse about that. Whatever it was you wanted to do, that isn't the way. What _do_ you want to do? Ask a question about that. – matt Nov 10 '17 at 23:00
  • It occurs to me to point out that you can make what amounts to an embed segue without using a container view - namely, make a _custom_ segue that _behaves_ like an embed segue. That way you can have your cake and eat it too: you have a segue that shows you the hierarchy in the storyboard, but we make no assumptions about where the view controller's view comes from. Does that satisfy you? – matt Nov 10 '17 at 23:21
  • I'm looking into how to reuse container view controller design in a storyboard. I want to design a container view controller in a storyboard and be able to use it again, in another screen in the same storyboard or a different storyboard, without drawing its view again. Does that make sense? – Srđan Stanić Nov 12 '17 at 16:06
  • So the spec is: the contents of this parent view controller's view are always the same, but the child view controller will be different depending on where we are in the hierarchy, yes? And you also want to be able to display the view controller relationships in the storyboard graphically somehow, yes? Well, I can tell you a way to do that. It's rather elaborate to configure, but it's do-able. – matt Nov 12 '17 at 16:13
  • Yes, a specific use case would be to create my own version of a tabbar controller or a navigation bar controller that I can reuse across multiple storybords. How would you do it? Would it be better to open a separate question? – Srđan Stanić Nov 12 '17 at 16:22
  • The problem is that the storyboard _knows_ about navigation controller and tab bar controller as _special cases_. It knows how they work and what interface they provide. That's why it shows their interface and provides "relationship" segues for them. That's why it can even show their interface _in the child view controller_. Nothing like that is going to happen for a completely custom parent view controller. Therefore the solution is always going to be a compromise. But I can suggest a way to make that compromise. – matt Nov 12 '17 at 16:26
  • The missing piece of the puzzle is that you're going to have to write (at the very least) a Custom segue. That is the only way to have a segue lead from view controller A to its child view controller B and yet have this meaning that is completely up to you. Let me know if that's not enough of a hint. :) It will be a triggered segue, not a relationship segue, but you can easily make it work like a relationship segue (with some extra code, of course). – matt Nov 12 '17 at 16:30