0

I am answering my own question in this post.

Requirement: I want to have a tab view controller as the top parent. This will have 5 tabs. In the first tab, I want to have a segment control at the top which I want to switch the views within this first tab with information based off of which segment index is selected.

Reading a few suggestions online were to-

  1. Either use containment view controllers where the first tab holds strong references to the child view controllers and switches them based off of which index is selected in the segment control. Problem with this is that each of the view controller will be eating up memory as we are keeping them in strong reference.

  2. Second idea I read online was to put everything on a single view controller in the first tab - based on which index segment control is selected, just hide everything else. Problem with this was super messy code with too much stuff on the same view controller plus storyboard would get messy with things on top of each other.

1 Answers1

0

Solution I came up was to embed another tabbarcontroller inside the first tab's view controller. Hide this second tab bar. This second tab bar will have the 3 child view controllers - each for each segmentcontrol's index. Whenever the segment is changed, I change the tab.

So in the second tabbarcontroller (self is the second tabbarcontroller)

-(void)segmentChanged:(UISegmentedControl*)sender{
    NSLog(@"New value: %d",sender.selectedSegmentIndex);
    [self setSelectedIndex:sender.selectedSegmentIndex];
}

This way iOS will put the view controllers in memory only when the segments are switched and not from the very beginning. Also iOS UIKit will handle the memory management for the tabs as mentioned here.

Plus we don't have to deal with messy code and storyboard shenanigans. Each segment control's index logic is separate in it's own view controller from the second tab.

Community
  • 1
  • 1
  • This is much the same as option 1. You can simply lazy-load the contained view controller as required and release the non-active ones if you get a low memory notification – Paulw11 Oct 08 '15 at 02:46
  • it sort of is and sort of isn't. UIKit will do a much decent job of releasing any views if it needs to, plus I don't have to have extra code for handing the memory stuff myself and also figuring out which one is active and only release the other ones or should I release all other ones or only one of them etc. So much lesser and cleaner, easier code. – sudoExclaimationExclaimation Oct 08 '15 at 02:49
  • 1
    ok.. tab bar.. hide the bottom bar... maybe Apple introduces some swipe gestures as for navigation controller and in future version your users switch view controllers without pushing the segmented control...That does not sound as the perfect place for the tab bar controller. What about the simple page view controller approach? – Fawkes Oct 08 '15 at 03:24
  • @Fawkes hmmm...you do bring up a good idea which I completely forgot about. I am actually going to give this page view controller a shot to see if that works for me too. If it works, it is a better solution than my solution of using tab bar controller for sure. I will reply with my results. – sudoExclaimationExclaimation Oct 08 '15 at 03:54
  • ViewDidUnload hasn't been called since iOS 6. Don't depend on it to "manage memory". – quellish Oct 08 '15 at 05:57