3

I have a storyboard with a navigation controller that leads to an UIVIewController that I want to reuse. That UIVIewController has a ParentUIViewController that has all the basic functionalities for all the UIVIewControllers that I am reusing.

Currently I am copying and pasting (meh) and then I change the class of the UIViewController to the ChildUIVIewController that I want to use (ChildUIViewController extends ParentUIViewController).

But this sounds like a bad solution. Everytime I want to change the ParentViewController visually I need to update, manually, all other ChildViewControllers.

I have tried to create a xib for the ParentViewController but the xib isn't loaded because I need a xib with the name of the ChildViewController. I have created it and then said the class is the ParentViewController but it crashes in the segue.

EDIT

I have created an example of the status of my problem

https://github.com/tiagoalmeida/storyboardexample

Note that the ParentViewController has a set of logic way more complicated that is not illustrated there. Also note that I am also using a TableView. I hope that this can illustrate the problem.

Community
  • 1
  • 1
Tiago Almeida
  • 14,081
  • 3
  • 67
  • 82

2 Answers2

1

Keep the logic on the parentViewController and the UI Part on the child UIViewControllers. If you need to create a new UIViewController, you will create a child that will have a corresponding XIB (or get rid of XIBs and create the interface by hand).

Rui Peres
  • 25,741
  • 9
  • 87
  • 137
  • And I have to copy and paste the views for every child? That is the same that having them in the storyboard :/ – Tiago Almeida Jul 19 '13 at 08:30
  • No, if two Childs share the same UI, put the File's Owner as your `ParentViewController`. If two childs share most of the same code, create a new `ParentViewController`. So you will have something like: `childViewController : parentViewController : grandParentViewController`. In this case `grandParentViewController` would have what's common to every `UIViewController` and the new `parentViewController` would hold the logic for the two child. – Rui Peres Jul 19 '13 at 08:33
  • Every child shares the same UI. So you are suggesting I create a xib for every child and put the file's owner as the ParentViewController (I was putting the File's owner as the ChildViewController)? I became kinda lost on your grandParentViewController logic. – Tiago Almeida Jul 19 '13 at 08:38
  • So what's does change between the children if they share the UI? A bit of the logic? If the logic is the only thing that changes the UI is the same, keep the UI on the `parentViewController` (and everything that is common to the children). The grandParentViewController logic, is keep drilling down the hierarchy so you can keep common code on a single place. – Rui Peres Jul 19 '13 at 08:40
  • It changes some logic (like the fetch for the server is kinda different, the title in the view, etc). Even with the scheme I have (separate xibs) I still have to copy paste the view because it crashes if there is no view in a UIVIewController. This solution seems so messy :/ – Tiago Almeida Jul 19 '13 at 08:45
  • Contact me on my email, so we can share some thoughts about that. :) – Rui Peres Jul 19 '13 at 08:48
0

Have you considered looping back into the same UIViewController via a "phantom button"?

Have a look at this: UIStoryboard Power Drill, Batteries included

Essentially you can drag a Bar Button Item into the little black bar under the View Controller in Storyboard (the 1 with View Controller, First Responder, and Exit icons; sorry, I don't recall what this is called exactly), then you can control+drag from that button back into the UIViewController for a Push segue. This should create a loop segue in your Storyboard. All you need to do next is give that segue an identifier, programmatically call it from your code using [self performSegueWithIdentifier:], then implement -(void)prepareForSegue: and use [segue destinationViewController] to conditionally set the title and perhaps some flags so you can identify when to use different kinds of fetches (or other code variations) in the same Class code.

Yazid
  • 970
  • 7
  • 14
  • This is interesting, thanks for the post. However, I believe that is easier to understand the code if I separate it in a class hierarchy. However, this is a possible approach and maybe I will use it if I can't find a better solution. – Tiago Almeida Jul 19 '13 at 13:51