0

Quite possibly I'm missing something obvious, but I have a very simple requirement and neither of these two seems to meet it.

I have an application with 3 distinct views, however for reasons I won't go into here (essentially views may or may not be valid depending in what is done in other views), using persistant tabs as navigation will not work. I also want each view to be created as needed and UITabBarController creates all its views upfront. However I still want some of the functionality of UITabBarController - being able to pass it in an Array of UIViewControllers.

UINavigationController offers the chance to have it create its views as needed, but there is no way for me to pass a list of views to it up front, so I end up with ViewControllers creating and pushing other (sibling) ViewControllers which is nasty.

So here are my requirements:

  1. I want to be able to add ViewControllers upfront
  2. I want to be able to navigate between the view controllers
  3. I want each ViewController to be created as needed and destroyed when navigated away from

Is there anything that fits the bill?

Undistraction
  • 42,754
  • 56
  • 195
  • 331
  • Does it mean that appearance of other views depend on what you do in active view before initializing them? – Eimantas Oct 04 '11 at 13:26
  • Basically. There are 3 views, but view 2 can't be accessed until certain actions have been performed in view 1, and view 3 can be accessed from view 2 and can possibly accessed from view 1 depending on actions taken in 1 and 2. Essentially the metaphor of tabs will not work because the views aren't all accessible all the time and it would only make sense to go from view1 - view3 using alternative UI. – Undistraction Oct 04 '11 at 13:48
  • That's what navigation controller is there for. You should skip the tabs (or visually represent them in first view) and go with navigation controller. – Eimantas Oct 04 '11 at 13:59
  • That doesn't solve requirement 1. – Undistraction Oct 04 '11 at 14:41
  • Unless creating controllers is very expensive operation I don't see how creating controllers on the go might be a problem. – Eimantas Oct 04 '11 at 14:54
  • They are expensive yes. I'm thinking about wrapping a UINavigationController and having an initWithViewControllers method on it, then having it manage Creation/Push and Pop/Destruction. I suppose I also want to get away from the situation that seems to be condoned where ViewControllers end up knowing about other sibling viewControllers just because they have to create them. Just because you clicked a button in the View of ViewController1 doesn't mean ViewController1 should know about the view you want to navigate to, let alone create it. That is just bad architecture. – Undistraction Oct 04 '11 at 15:18

1 Answers1

0

I would use the AppDelegate to create a hash of viewcontrollers upfront and create a navigation controller. Once you click the button to navigate to a new viewcontroller you can go to the hash and look for that specific view controller. If it is not created you can create it there. If you want to destroy that view controller just release it and remove from the hash.

Tony
  • 10,088
  • 20
  • 85
  • 139