Reputation: 517
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];
}
Upvotes: 2
Views: 1499
Reputation: 7065
Look here at the view property
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!
Upvotes: 5
Reputation: 31016
This is a problem:
ShowBookDetails *sbd = [[ShowBookDetails alloc] initWithNibName:@"ShowBookDetails" bundle:nil];
If you're not using ARC it's a memory leak because you have no way to release the object once the method containing that line ends.
If you are using ARC, it will automatically release sbd
and anything in its view that refers to the controller will be trying to use a dealloc'd object.
You should make ShowBookDetails *sbd
a strong (or retain) property in this class rather than a local variable.
Upvotes: 0
Reputation: 4921
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
ShowBookDetails *sbd = [[ShowBookDetails alloc] initWithNibName:@"ShowBookDetails" bundle:nil];
[self switchToView:sbd from:self.view];
}
-(void)switchToView:(UIViewController*)nextView from:(UIView*)currentView{
[currentView insertSubview:nextView.view atIndex:1];
}
Modify to this code
Upvotes: 0