Reputation:
I am new to using this method so I could be doing this completely wrong so here is my code:
@property (nonatomic, weak) ConverterViewController *converterViewController;
@property (nonatomic, weak) CalculatorViewController *calculatorViewController;
If I am understanding this code correctly, these are acting as references to Two different ViewControllers.
Then I have this in my viewDidAppear method:
[self addChildViewController:_converterViewController];
[_converterViewController didMoveToParentViewController:self];
[self.view addSubview:_converterViewController.view];
I am getting an NSException at the first line when I try and add it as a child view controller. So not knowing whether or not this should then call some methods in my ConverterViewController class I put some breakpoints within that class both the initWithNibName and viewDidLoad methods and I found that neither of these methods are being called, so Im not exactly sure what is wrong. Then again Im not really sure what could go wrong so any help is greatly appreciated.
This is all I get from the console:
libc++abi.dylib: terminating with uncaught exception of type NSException
Upvotes: 2
Views: 282
Reputation: 20234
Updated Answer:
[self addChildViewController:_converterViewController];
does not create the converterViewController
.
It simply takes the converterViewController
object and adds it as a childViewController
to self
.
You will need to allocate memory and instantiate the object converterViewController
before -addChildViewController:
or else it's value will be nil
and nothing will happen.
So... something this:
_converterViewController = [[ConverterViewController alloc] initWithNibName:@"ConverterViewController"
bundle:nil];
//now... adding it as childViewController should work
[self addChildViewController:_converterViewController];
[_converterViewController didMoveToParentViewController:self];
//optional: give it a frame explicitly so you may arrange more childViewControllers
//[_converterViewController.view setFrame:CGRectMake(0,0,100,100)];
[self.view addSubview:_converterViewController.view];
Upvotes: 2