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?

7
  • 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. Commented Jul 18, 2014 at 21:00
  • I created these views in storyboard, and I would like to keep that way... Is there a way? Commented Jul 18, 2014 at 21:18
  • Have you looked into container views? Commented Jul 18, 2014 at 21:28
  • Ok, I made it an answer explaining a bit more... Commented Jul 18, 2014 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?? Commented Jul 18, 2014 at 21:38

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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!
One other thing... From my MainViewController, how do I get to the other controllers? I can only get outlets to the views...
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: stackoverflow.com/questions/13279105/…
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.

2 Comments

reading pnavks answer below, you should consider using these container view controls instead of my suggested placeholder views :)
Yes, indeed his approach is better! =) but thank you for your reply, you do get my up vote ;)
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.

2 Comments

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.
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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.