I have a problem when trying to push a new view controller onto an existing navigation controller.
The thing I'm trying is to make a UIPopoverController
appear when pushing a navigation UIBarButtonItem
, and from that "dropdown" select a menu point which will push the associated view controller onto the "main" navigation controller.
I've tried the following, which gives a modal. But I want the view pushed.
If selecting push instead of modal the result is as following.
I've also tried making a custom
UITableViewController
(on the popover) from which I've tried the following code:
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UINavigationController *dash = [storyboard instantiateViewControllerWithIdentifier:@"dash_nav"];
UIViewController *students = [storyboard instantiateViewControllerWithIdentifier:@"students"];
if (indexPath.row == 0) {
[dash pushViewController:students animated:YES];
// [[dash navigationController] presentViewController:students animated:YES completion:nil];
}
NSLog(@"%@", [dash title]);
NSLog(@"index = %i", indexPath.row);
}
Is there a way to do what I am trying to accomplish?
This code:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UINavigationController *dash = [storyboard instantiateViewControllerWithIdentifier:@"dash_nav"];
UIViewController *students = [storyboard instantiateViewControllerWithIdentifier:@"students"];
is creating too many new instances. You should be using the existing storyboard (self. storyboard
) and the existing navigation controller. The navigation controller needs to be passed to the table view controller (which you should use because the storyboard doesn't have the required information). We'll call this originatingNavigationController
, a new @property
on the table view controller.
When the segue triggers to show the popover, set the navigation controller reference into the destination view controller (the table view).
Then, in the didSelectRowAtIndexPath:
method you just instantiate the students
VC and push it:
UIViewController *students = [self.storyboard instantiateViewControllerWithIdentifier:@"students"];
[self.originatingNavigationController pushViewController:students animated:YES];
and then the table view controller should dismiss itself (its popover).