38

I have a UIView->UICollectionView->UICollectionViewCell. I am trying to navigate back programatically but none of these works. The code did called. I am using StoryBoard.

- (void) goBack:(NSNotification *) notification {
      // [self.navigationController popViewControllerAnimated:YES];
     //  [self dismissViewControllerAnimated:YES completion:nil];
      [self.navigationController popToRootViewControllerAnimated:YES];
}
Bartłomiej Semańczyk
  • 59,234
  • 49
  • 233
  • 358
user1688346
  • 1,846
  • 5
  • 26
  • 48

9 Answers9

103

You need to use:

[self.navigationController popToRootViewControllerAnimated:YES];

This will bring you back to the root view controller.

If you want to navigate back to previous view controller, you should implement:

[self.navigationController popViewControllerAnimated:YES];
Undo
  • 25,519
  • 37
  • 106
  • 129
Vineet Singh
  • 4,009
  • 1
  • 28
  • 39
19

By using below line we can go to parent view controller

[self.navigationController popViewControllerAnimated:YES]; 

By using below line we can move to main/root view controller

[self.navigationController popToRootViewControllerAnimated:YES]; 

By using below line we can move to any view controller

[self.navigationController popToViewController:viewControllerObject animated:YES]; 
Madhu
  • 2,565
  • 2
  • 25
  • 32
4

How about...

 [self.navigationController dismissViewControllerAnimated:YES completion:NULL];

Assuming that you are currently in a navigation-based controller and you wanted to go back to the previous controller before you got into a navigation-based controller.

JCurativo
  • 713
  • 6
  • 17
4

With swift3,

@IBAction func back(_ sender: UIButton) {
    self.dismiss(animated: true, completion: nil)     
}
LF00
  • 27,015
  • 29
  • 156
  • 295
4

Swift 4.1:

navigationController.popViewController(animated: true)
Kristian
  • 2,071
  • 23
  • 15
3

Swift solutions for easy copy pasting:

navigationController?.popViewControllerAnimated(true)
Esqarrouth
  • 38,543
  • 21
  • 161
  • 168
2

Try it....

#import "bookdescriViewController.h" // import here your class name 

- (IBAction)backButton:(id)sender 
{
  bookdescriViewController *previosVC = [[bookdescriViewController alloc]init];
  [self.navigationController popViewControllerAnimated:YES]; // go to previous view controller
  [self.navigationController popToRootViewControllerAnimated:YES]; // go to root view controller
  [self.navigationController popToViewController:previosVC animated:YES]; // go to any view controller
  [previosVC release];
}
saurabh
  • 6,687
  • 7
  • 42
  • 63
Chirag Pipaliya
  • 1,281
  • 12
  • 20
0

Go back to parent view controller and dealloc current view controller ex :

- (void)applicationDidEnterBackground:(NSNotification *)notification
{
    NSInteger numberOfViewControllers = self.navigationController.viewControllers.count;

    UIViewController *vc = [self.navigationController.viewControllers objectAtIndex:numberOfViewControllers - 2];

    [self.navigationController popToViewController:vc animated:NO];
}

or another view controller

Alan10977
  • 973
  • 1
  • 9
  • 7
-1
- (void) goBack:(NSNotification *) notification 
{ 
   if(!self.YOrView.isHidden)
      self.YOrView.hidden = YES;
}