I have a UITableViewController within a UIViewController. While this table viewcontroller was the only one involved, it was pushing views just fine when the user would tap a row. However, ever since I moved it to be one of two contained within the UIViewController, the taps of rows suddenly do nothing.
I’ve tried searching around and I’m not the first to run into this problem, but none of the answers fit my circumstances or the questions have no working answers. That link was the closest I found, but I’m not using storyboards — I’m using separate XIBs.
So how do I push a new view from a viewcontroller within a viewcontroller?
To recap:
-
Here is what I had, and it worked fine in taking users to a new screen!
// Normal table behavior, as illustrated by [another question][2]. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { SomeView *detailViewController = [[SomeView alloc] initWithNibName:@"SomeView" bundle:nil]; // Pass the selected object to the new view controller. [self.navigationController pushViewController:detailViewController animated:YES]; } -
Now I have the viewcontroller as a property in a view — and the above code, which is in the file for the tableviewcontroller and not at the “main” view, doesn’t cause a new screen to appear anymore!
Thanks for the comments! Here’s some code to clarify my scenario.
-
The controllers within a controller. This is a file from a test project I’ve been using to test the concept out. In this case, I have a tableview controller within a tableview controller.
@interface SimpleTableViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> // This is the controller within the controller @property IBOutlet SecondTableViewController *secondTableController; @property IBOutlet UITableView *secondTable; -
My
SecondTableViewControllerhas this fun bit.- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // Navigation logic may go here. Create and push another view controller. UIViewController *detailViewController = [[UIViewController alloc] initWithNibName:@"SimpleNonTableViewController" bundle:nil]; // ... // Pass the selected object to the new view controller. [manualViewControllerParent.navigationController pushViewController:detailViewController animated:YES]; }
The view that the user interacts with is hooked up to SimpleTableViewController. In this way, SecondTableViewController is “within” SimpleTableViewController. Feel free to comment if you’d like more details!
I’ve put my test/concept project on github. https://github.com/hyliandanny/TableViewCeption
You need to use a custom container controller to do what you want. It would be easiest if you used a storyboard, but you can do it in code with xibs as well. The outer controller should be a UIViewController, not a table view controller. You can do something like this (in the outer controller):
You should read up on Apple’s documentation for custom container controllers.