Michael Lee
Michael Lee

Reputation: 91

How to dismiss a TableViewController within code called by a storyboard push segue?

I know how to handle push segues in Storyboard and the use of the back button that is created with the navigation controller. I have one main tableview connected to a child tableview via push segue in Storyboard. Works fine for switching between tables.

I want to add the ability to also gesture swipe left in the child tableview to return to main tableview.

First tried this within Storyboard with the use of the swipe gesture recognizer, but this caused the main tableview to become a new child to the child tableview.

I then tried within code:

[self.presentingViewController dismissViewControllerAnimated:YES completion:nil];

... but this seems to only work with a modal segue

I have:

- (void)viewDidLoad
{
   [super viewDidLoad];

   UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
   [recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];
   [self.tableView addGestureRecognizer:recognizer]; 
}

- (void)handleSwipe:(UISwipeGestureRecognizer *)gestureRecognizer
{
   // need code here to dismiss the child tableview
}

Upvotes: 3

Views: 1233

Answers (1)

Michael Lee
Michael Lee

Reputation: 91

Finally figured it out today and it works perfectly:

- (void)handleSwipe:(UISwipeGestureRecognizer *)gestureRecognizer
{
    [self.navigationController popViewControllerAnimated:YES];
}

Found answer in:

iOS Developer Library > UINavigationController Class Reference > popToViewController

Upvotes: 4

Related Questions