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];
}
4

9 回答 9

103

你需要使用:

[self.navigationController popToRootViewControllerAnimated:YES];

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

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

[self.navigationController popViewControllerAnimated:YES];
于 2013-04-18T07:18:15.240 回答
19

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

[self.navigationController popViewControllerAnimated:YES]; 

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

[self.navigationController popToRootViewControllerAnimated:YES]; 

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

[self.navigationController popToViewController:viewControllerObject animated:YES]; 
于 2013-11-21T12:09:48.500 回答
4

怎么样...

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

假设您当前处于基于导航的控制器中,并且您想在进入基于导航的控制器之前返回上一个控制器。

于 2013-04-18T07:13:40.973 回答
4

使用 swift3,

@IBAction func back(_ sender: UIButton) {
    self.dismiss(animated: true, completion: nil)     
}
于 2017-08-07T07:42:41.310 回答
3

Swift solutions for easy copy pasting:

navigationController?.popViewControllerAnimated(true)
于 2015-03-01T16:39:51.337 回答
3

斯威夫特 4.1:

navigationController.popViewController(animated: true)
于 2018-08-13T21:15:24.547 回答
2

试试看....

#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];
}
于 2013-04-18T06:11:42.353 回答
0

返回父视图控制器并释放当前视图控制器 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];
}

或另一个视图控制器

于 2015-03-13T23:51:19.103 回答
-1
- (void) goBack:(NSNotification *) notification 
{ 
   if(!self.YOrView.isHidden)
      self.YOrView.hidden = YES;
}
于 2013-04-18T06:00:52.550 回答