This one is probably something simple, still learning the ins-and-outs on this but I’ve run out of searches for this one with no available answer.
I’ve got a UIViewController with several elements displayed on it, one such element is a UITableView. The UITableView has it’s own class and is allocated in the UIViewControllers viewWillAppear
- (void)viewWillAppear:(BOOL)animated
{
UITableView *insideTableView = [[UITableView alloc] init];
tableView.delegate = insideTableView;
tableView.dataSource = insideTableView;
}
Everything is working fine in regards to the tableview. Today I am experimenting with a few additions, one of which is a new view popup on cell selection within that tableview.
Inside my TableView Class, I have the following:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"Cell Pressed, Present View");
PopupView *popupView = [[PopupView alloc] initWithNibName:@"PopupView" bundle:nil];
popupView.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:popupView animated:YES];
}
Now it gets called fine, verified by the NSLog, however the view doesn’t appear. I know the problem is related to the fact that I want PopUp to appear over the TableViews Parent rather than itself.
I’m just not sure how to properly call it in this instance.
The delegate is a
UIViewControllerwhich doesn’t have itsviewproperty set, which is whypresentModalViewController::doesn’t work.You need the view controller containing the table view to present the modal view controllers, but note that that view controller is not the parent of the table view delegate. This is because you have no view controller hierarchy in place.
The easiest way to fix this is to put those methods inside the view controller whose view contains the table view. Alternatively the table view delegate needs to hold a reference to the view controller so it can call
presentModalViewController::on it.The latter approach can lead to retain cycle, so you have to use a non-retaining reference. The nicest implementation is the delegate pattern.
Also, you don’t want to do the instantiation in
viewWillAppear:because that can be called multiple times during the lifecycle of a view controller. Put the code inviewDidLoadand balance it indealloc. Right now you are leaking memory every time your view appears, which when your modal view controller is working will be every time the modal view controller is presented and dismissed.