user3672051
user3672051

Reputation:

How to add child view as a subview of a view controllers view?

So far I have added a child view to my main view controller. however I want this child view to be a subview of my view controllers view so I tried to do it like this

[self.view addSubview:_converterViewController.view]; 

but this is giving me this error:

libc++abi.dylib: terminating with uncaught exception of type NSException

I know this is really vague but for some reason this is all I ever get. so what am I doing wrong? here is the code I have related to this task, and it is all located in my view controllers viewDidAppear method.

[self addChildViewController:_converterViewController];
[_converterViewController didMoveToParentViewController:self];
[self.view addSubview:_converterViewController.view];//this line produces the error

Thanks for the help.

Updated Error:

'NSInternalInconsistencyException', reason: 'Could not load NIB...

Upvotes: 1

Views: 995

Answers (3)

user3672051
user3672051

Reputation:

For my situation I had avoided having multiple views as part of the same view controller for UI design in IB however I reverted to this method after fixing my design issues. This eliminates all of the code needed for using child view controllers and renders my need for a solution to this problem void. I did this shortly before staticVoidMan posted his possible solution and I have no intention of going through the trouble of seeing if his solution works.

despite my decision to use other methods I greatly appreciate all the help I received on the matter.

Upvotes: 0

staticVoidMan
staticVoidMan

Reputation: 20234

For storyboard, do:

//instead of
//[[ConverterViewController alloc] init];
//do
//in my case, Storyboard name was "Main"
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
_converterViewController = [storyboard instantiateViewControllerWithIdentifier:@"ConverterViewController"];

//add as childViewController
[self addChildViewController:_converterViewController];
[_converterViewController didMoveToParentViewController:self];
//optional: modify parameters if needed
[_converterViewController.view setBackgroundColor:[UIColor redColor]];
[_converterViewController.view setFrame:CGRectMake(0, 64, 320, 100)];
[self.view addSubview:_converterViewController.view];

Assumptions:

  1. Name of storyboard is "Main" (yours might be just "Storyboard")

    • Project Targets
  2. Storyboard Identifier of ConverterViewController in storyboard is ConverterViewController

    • Identity Inspector

Upvotes: 0

Kostiantyn Koval
Kostiantyn Koval

Reputation: 8483

Here are steps that you need to do when adding childViewController

UIViewController *viewController = [UIViewController new];

[self addChildViewController:viewController];
[self.view addSubview:viewController.view];
[viewController didMoveToParentViewController:self];

1 - Create Child View Controller
2 - Add is as a child
3 - Add it's view to the view hierarchy 4 - Notify childViewController that it has been added to the parent.

Upvotes: 2

Related Questions