导航回到上一个视图控制器

我有一个UIView->UICollectionView->UICollectionViewCell 。 我正在尝试导航回编程,但没有这些作品。 代码确实叫。 我正在使用StoryBoard。

 - (void) goBack:(NSNotification *) notification { // [self.navigationController popViewControllerAnimated:YES]; // [self dismissViewControllerAnimated:YES completion:nil]; [self.navigationController popToRootViewControllerAnimated:YES]; } 

你需要使用:

 [self.navigationController popToRootViewControllerAnimated:YES]; 

这将使您回到根视图控制器。

如果你想导航回前一个视图控制器,你应该实现:

 [self.navigationController popViewControllerAnimated:YES]; 

通过使用下面的行,我们可以去父视图控制器

 [self.navigationController popViewControllerAnimated:YES]; 

通过使用下面的行,我们可以移动到主/根视图控制器

 [self.navigationController popToRootViewControllerAnimated:YES]; 

通过使用下面的行,我们可以移动到任何视图控制器

 [self.navigationController popToViewController:viewControllerObject animated:YES]; 

怎么样…

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

假设您目前处于基于导航的控制器中,并且您希望在进入基于导航的控制器之前回到之前的控制器。

Swift解决scheme,便于复制粘贴:

 navigationController?.popViewControllerAnimated(true) 

尝试一下….

 #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]; } 
 - (void) goBack:(NSNotification *) notification { if(!self.YOrView.isHidden) self.YOrView.hidden = YES; } 

回到父视图控制器,并释放当前视图控制器前:

 - (void)applicationDidEnterBackground:(NSNotification *)notification { NSInteger numberOfViewControllers = self.navigationController.viewControllers.count; UIViewController *vc = [self.navigationController.viewControllers objectAtIndex:numberOfViewControllers - 2]; [self.navigationController popToViewController:vc animated:NO]; } 

或另一个视图控制器

用swift3,

 @IBAction func back(_ sender: UIButton) { self.dismiss(animated: true, completion: nil) }