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

时间:2013-04-18 05:59:12

标签: ios objective-c uinavigationcontroller

我有UIView->UICollectionView->UICollectionViewCell。我试图以编程方式返回,但这些都不起作用。代码确实调用了。 我正在使用StoryBoard。

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

9 个答案:

答案 0 :(得分:97)

您需要使用:

[self.navigationController popToRootViewControllerAnimated:YES];

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

如果要导航回上一个视图控制器,则应实现:

[self.navigationController popViewControllerAnimated:YES];

答案 1 :(得分:17)

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

[self.navigationController popViewControllerAnimated:YES]; 

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

[self.navigationController popToRootViewControllerAnimated:YES]; 

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

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

答案 2 :(得分:5)

怎么样......

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

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

答案 3 :(得分:3)

Swift解决方案,可轻松复制粘贴:

navigationController?.popViewControllerAnimated(true)

答案 4 :(得分: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];
}

答案 5 :(得分:2)

Swift 4.1:

navigationController.popViewController(animated: true)

答案 6 :(得分:1)

使用swift3,

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

答案 7 :(得分:0)

- (void) goBack:(NSNotification *) notification 
{ 
   if(!self.YOrView.isHidden)
      self.YOrView.hidden = YES;
}

答案 8 :(得分: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];
}

或其他视图控制器