xcode IOS:打开和关闭一些视图控制器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9031481/
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-09-14 23:07:20  来源:igfitidea点击:

IOS: open and close some view controllers

iosxcodeuiviewcontrolleruinavigationcontrollerpresentmodalviewcontroller

提问by cyclingIsBetter

In my app I have the following configuration to open my view controllers:

在我的应用程序中,我有以下配置来打开我的视图控制器:

When I write "pushViewController"I use a navigation controller, and when I write "present"I use a presentModalViewController.

当我写“pushViewController”时我使用导航控制器,当我写“present”时我使用一个presentModalViewController.

firtsView -> (pushviewcontroller) -> secondOneView -> (present) -> thirdOneView -> (present) -> fourthView

firstView -> (pushviewcontroller) -> secondTwoView -> (present) -> thirdTwoView 

This is the scheme of my app to organize my view controllers. Then my question is:

这是我的应用程序组织视图控制器的方案。那么我的问题是:

What's the way to return from "fourthView"(that is when I go back from "fourthView") to "secondTwoView"?

“fourthView”(即我从“fourthView”返回时)返回到“secondTwoView”的方法是什么?

Is there a way to do it?

有没有办法做到这一点?

回答by

Yes there are.

是的。

The UIViewControlleroffers different methods to dismiss a view controller depending on if you presented it modally or not. These are :

UIViewController取决于如果您呈现它有模式或不提供不同的方法来解雇一个视图控制器。这些是 :

-(void)dismissModalViewControllerAnimated:(BOOL)animated; // modal
-(void)dismissViewControllerAnimated:(BOOL)flag
                          completion:(void (^)(void))completion;

You will need to dismiss them one by one. Also, take time to read the View Programming Guidefrom Apple.

您将需要一一解雇它们。另外,花点时间阅读Apple的View Programming Guide

Using a UINavigationControlleryou may pop to any view controller using :

使用 aUINavigationController您可以使用以下命令弹出任何视图控制器:

-(NSArray *)popToViewController:(UIViewController *)viewController
                       animated:(BOOL)animated;

Alternatively, another method let you pop just one :

或者,另一种方法让您只弹出一个:

-(UIViewController *)popViewControllerAnimated:(BOOL)animated;

回答by IPaPa

if you use presentViewController

如果你使用presentViewController

i think you have to dismiss viewController one by one

我认为你必须一一关闭 viewController

but if you navigationcontroller only, then you can pop to viewController you want

但是如果你只使用导航控制器,那么你可以弹出你想要的视图控制器

回答by Zitao Xiong

  1. in thirdOneView's viewcontroller, do dismissModalViewControllerAnimated:NO
  2. in secondOneView's viewcontroller do dismissModalViewControllerAnimated:NO again.
  3. in secondOneView's viewcontroller do popViewControllerAnimated:NO
  4. in firstView's Viewcontroller do pushViewController to secondTwoView
  1. 在thirdOneView 的viewcontroller 中,执行dismissModalViewControllerAnimated:NO
  2. 在 secondOneView 的 viewcontroller 中再次 DismissModalViewControllerAnimated:NO。
  3. 在 secondOneView 的视图控制器中执行 popViewControllerAnimated:NO
  4. 在 firstView 的 Viewcontroller 中执行 pushViewController 到 secondTwoView

If you want some animation. I would suggest to do it manually by using CoreAnimation. Since

如果你想要一些动画。我建议使用 CoreAnimation 手动完成。自从

-(void)dismissViewControllerAnimated:(BOOL)flag
                      completion:(void (^)(void))completion;

is only available after iOS5.

仅在 iOS5 之后可用。