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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16075525/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:15:01  来源:igfitidea点击:

Navigate Back to previous view controller

iosobjective-cuinavigationcontroller

提问by user1688346

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.

我有一个UIView->UICollectionView->UICollectionViewCell. 我正在尝试以编程方式返回,但这些都不起作用。代码确实调用了。我正在使用故事板。

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

回答by Vineet Singh

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

回答by Madhu

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

回答by Jenn Eve

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.

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

回答by Esqarrouth

Swift solutions for easy copy pasting:

用于轻松复制粘贴的 Swift 解决方案:

navigationController?.popViewControllerAnimated(true)

回答by Kristian

Swift 4.1:

斯威夫特 4.1:

navigationController.popViewController(animated: true)

回答by Kris Roofe

With swift3,

使用swift3,

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

回答by Chirag Pipaliya

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

回答by Alan10977

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

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

或其他视图控制器

回答by Alan10977

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