Search code examples
objective-cxcodeios7uiviewcontrolleruistoryboard

Need clarity on if I'm switching View Controllers correctly


I'm making an ipad app which has 11 view controllers in storyboard, one of which is a master view that contains buttons to control which of the other view controllers is displayed as its subview.

Does anybody know for sure if it's ok to use UIViewControllers as subviews of other UIViewControllers, with hard evidence from apple or another source? It has been working flawlessly for me using this method so far:

    int nextPage = (method to determine nextPage based on button pressed);
    [currentView.view removeFromSuperview];
    currentView = [self.storyboard instantiateViewControllerWithIdentifier:[NSString  stringWithFormat:@"page%i",nextPage]];
    [self.view insertSubview:currentView.view atIndex:0];
    [currentView.view setFrame:CGRectMake(0, 0, 1024, 768)];

Some people online say that if you are using ios5 or later, it's ok, but others say "NO NEVER DO THIS!!!" even if it's ios5. Others say to use container views, but in every tutorial I've seen to use a container view you just end up inserting the view controller as a subview to the main view anyway using this after you've inserted the child view:

 [self.view addSubview:self.currentView];

I am not using a Navigation Controller because they have limited customizability and I do not want any stock tab bars or navigation bars, just all custom buttons.

Thanks in advance!


Solution

  • Yes of course, it's how UINavigationController works and how UITabBarController works. Interface Builder will event set it up for you with a container view.

    see Creating Custom Container View Controllers


    The takeaway is it's important to handle childViewControllers. This can get a bit tricky, but is important for notifications to propagate correctly.