When i use the method insertSubview:atIndex: on my iPhone the program fails to run, with EXC_BAD_ACCESS in the main.m file. However, when i use presentModalViewController the program runs perfectly.
Also, the method switchToView works when its first used, with a different to and from, but the second time it doesn’t.
What is going wrong?
Here is my code:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
ShowBookDetails *sbd = [[ShowBookDetails alloc] initWithNibName:@"ShowBookDetails" bundle:nil];
[self switchToView:sbd from:self];
}
My method look like:
-(void)switchToView:(UIViewController*)nextView from:(UIViewController*)currentView
{
[currentView.view insertSubview:nextView.view atIndex:1];
}
Look here at the view property
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/Reference/Reference.html
It stricly states exactly what I stated in a comment:
“Each view controller object is the sole owner of its view. You must not associate the same view object with multiple view controller objects. The only exception to this rule is that a container view controller implementation may add this view as a subview in its own view hierarchy. Before adding the subview, the container must first call its addChildViewController: method to create a parent-child relationship between the two view controller objects.”
Straight from Apple!