0

I have a UIViewController (MainViewController) which manages a view with two subviews, which I created using storyboard. Now I want these two sub views to be managed by their own model controllers, so most of the work done by the app is distributed instead of being central to this one controller.

I tried setting the view on the view controllers, but it doesn't work (at least viewDidLoad on the other controllers does not get called, although the views show). In MainViewController's viewDidLoad I have this:

MainNavigationViewController* navigationViewController = [[MainNavigationViewController alloc] init];
MainContentViewController* contentViewController = [[MainContentViewController alloc] init];

navigationViewController.view = self.navigationView;
contentViewController.view = self.contentView;

How can I do this? And more, is this a good idea?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
André Fratelli
  • 5,920
  • 7
  • 46
  • 87
  • You could forward the calls to viewDidAppear etc. But I guess I would do it the other way round: creating the views in their ViewControllers and adding them to your combining ViewController. I think then the methods would be called, not 100% sure though. – robert Jul 18 '14 at 21:00
  • I created these views in storyboard, and I would like to keep that way... Is there a way? – André Fratelli Jul 18 '14 at 21:18
  • Have you looked into container views? – Douglas Jul 18 '14 at 21:28
  • Ok, I made it an answer explaining a bit more... – robert Jul 18 '14 at 21:36
  • @Douglas, you are the second person to tell me about those; the other guy talked about UIContainerView, but Xcode says its and undeclared identifier. What are those?? – André Fratelli Jul 18 '14 at 21:38
  • When editing a storyboard you can drag a `Container View` to your viewController. that creates you a placeholder like mentioned in my answer and a linked `UIViewController` in that you could put the layout of i.e. your navigationView – robert Jul 18 '14 at 21:45
  • @robert, I got it! I already accepted the answer =) thank you. – André Fratelli Jul 18 '14 at 21:48

3 Answers3

2

In Storyboard, you can place these subviews inside a Container View control which defines a region of a view controller that can include a child view controller. Then you have that child view controller manage the subviews.

pnavk
  • 4,552
  • 2
  • 19
  • 43
  • I have a very different programming background. I guess I have to get used to the fact that with Xcode things are just... Simple =) thank you so much! – André Fratelli Jul 18 '14 at 21:46
  • One other thing... From my MainViewController, how do I get to the other controllers? I can only get outlets to the views... – André Fratelli Jul 18 '14 at 22:55
  • 1
    Glad it worked for you! Sorry for such a late reply, but if you're still stuck on this, there is an EMBED segue which occurs when the child VC becomes a child of the Main View Controller, you can get a reference to the Child VC using this. Check out the accepted answer here: http://stackoverflow.com/questions/13279105/access-container-view-controller-from-parent-ios – pnavk Jul 21 '14 at 19:20
1

viewDidLoad is definitely not being called because the views are already loaded. If the other methods (viewWillAppear etc) are not being called, too, you could forward the calls to the other viewControllers.

I would go for the other way, that is adding views already having a viewController to the MainViewController. I'm not completely sure about this, but I think that the viewControllers methods will be called that way.

It should work as follows:

Create a storyboard for the MainNavigationViewController containing your navigation view. Create another one for the MainContentViewController containing the content view. Remove all the subviews from the storyboard of the MainViewController, but keep the navigationView and contentView as empty placeholders. Create two properties for the viewControllers in your mainViewController to store their references. In viewDidLoad of the MainViewController, create the other two viewControllers via

self.mainNavigationController = [[UIStoryboard storyboardWithName:@"MainNavigationControllersStoryboardName" bundle:nil] instantiateViewControllerWithIdentifier:@"MainNavigationControllerIdentifier"]; 
self.mainContentController = [[UIStoryboard storyboardWithName:@"MainContentViewControllersStoryboardName" bundle:nil] instantiateViewControllerWithIdentifier:@"MainContentViewControllerIdentifier"]; 

and add their views to the placeholders:

[self.navigationView addSubview: mainNavigationController.view];
[self.contentView addSubview: mainContentController.view];

I hope you're using auto layout so the views will adjust their size, otherwise you would have to set their frames in viewWillLayoutSubviews to match the frames of the parent views.

robert
  • 2,822
  • 1
  • 16
  • 19
  • reading pnavks answer below, you should consider using these container view controls instead of my suggested placeholder views :) – robert Jul 18 '14 at 21:38
  • Yes, indeed his approach is better! =) but thank you for your reply, you do get my up vote ;) – André Fratelli Jul 18 '14 at 21:47
0

I think you are looking for something like

[navigationViewController pushViewController:contentViewController animated:YES];

NavigationViewController has a stack of view controllers. You have to push your view controller into the navigation view stack.

veovo
  • 191
  • 7
  • Not at all. I did mention that I want to *set* the view controller on these views, as they have none. They are subviews; I don't want to navigate to them because they are already on screen. – André Fratelli Jul 18 '14 at 21:17
  • If I understand correctly, you are saying that you want the contentViewController to select and present either of the two sub views and each subview has its own view controller? – veovo Jul 18 '14 at 21:31