xcode 转到应用程序中的第一个视图控制器

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

Go to first view controller in app

iphoneiosxcodeuinavigationcontroller

提问by ScottF

I need to go to the first view in my app. I have a few views pushed onto the stack then a modal navigation controller and more views pushed onto that.

我需要转到我的应用程序中的第一个视图。我有一些视图推送到堆栈上,然后是一个模态导航控制器,还有更多视图推送到它上面。

The problem I'm having is that using [[self navigationController] popToRootViewControllerAnimated:YES];only goes back to the first view in the modal stack.

我遇到的问题是 using[[self navigationController] popToRootViewControllerAnimated:YES];只返回到模态堆栈中的第一个视图。

And I can't get [[self navigationController] popToViewController:..to work because the true first view controller isn't accesible with [[self navigationController] viewControllers].

而且我无法开始[[self navigationController] popToViewController:..工作,因为真正的第一个视图控制器无法通过[[self navigationController] viewControllers].

Any ideas on how to accomplish this? Thanks.

关于如何实现这一点的任何想法?谢谢。

采纳答案by danh

Do this:

做这个:

[[self navigationController] dismissModalViewControllerAnimated:YES];

That will get you back to the VC that modally presented the navigation controller. Getting farther back after that depend on how you pushed those "few views" before the navigation controller.

这将使您回到以模态呈现导航控制器的 VC。在那之后走得更远取决于您如何在导航控制器之前推送这些“少数视图”。

Edit- explanation to get to the deepest root...

编辑- 解释到最深的根源......

It sounds like those "few views" are on another, underlying navigation controller's stack. This can be a little tricky, because the clean way to get farther back in that stack is to have that underlying navigation controller pop to it's own root. But how can it know that the modal VC on top of it is done?

听起来那些“少数视图”位于另一个底层导航控制器的堆栈上。这可能有点棘手,因为在堆栈中更远的干净方法是让底层导航控制器弹出到它自己的根。但是它怎么知道它上面的模态VC已经完成了呢?

Let's call the view controller that did the modal presentation of second navigation controller VC_a. It's a modally presented navigation controller whose topmost VC is VC_b. How can VC_a know to pop to it's navigation root when VC_b modally dismisses itself?

让我们调用进行第二个导航控制器 VC_a 模态呈现的视图控制器。它是一个模态呈现的导航控制器,其最顶层的 VC 是 VC_b。当 VC_b 模态地关闭自己时,VC_a 如何知道弹出到它的导航根?

The good answer (usually) is that VC_b decided to dismiss itself for a reason - some condition in your app/model changed to make it decide to be done.

好的答案(通常)是 VC_b 出于某种原因决定关闭自己 - 您的应用程序/模型中的某些条件已更改以使其决定完成。

We want VC_a to detect this condition, too. When VC_b gets dismissed, and VC_a gets a viewWillAppear message because it's about to be uncovered:

我们也希望 VC_a 检测到这种情况。当 VC_b 被解雇时,VC_a 收到一条 viewWillAppear 消息,因为它即将被发现:

// VC_a.m

- (void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];
    if (/* some app condition that's true when VC_b is done */) {
        // I must be appearing because VC_b is done, and I'm being uncovered
        // That means I'm done, too.  So pop...
        [self.navigationController popToRootViewControllerAnimated:NO];
    } else {
        // I must be appearing for the normal reason, because I was just pushed onto the stack
    }
}

回答by jbook

You need to do it by using the delegation pattern. Specifically, by creating a protocol that implements the delegate's respondsToSelectormethod.

您需要使用委托模式来完成。具体来说,通过创建一个实现委托respondsToSelector方法的协议。

See this postfor complete details. It should be almost exactly what you are looking for. I had to do something similar, except I only needed to pop one view off the navigation stack instead of using popToRootViewControllerAnimated:.

有关完整的详细信息,请参阅此帖子。它应该几乎正是您要查找的内容。我不得不做类似的事情,除了我只需要从导航堆栈中弹出一个视图而不是使用popToRootViewControllerAnimated:.

回答by Paras Joshi

In AppDelegate.m class create method with bellow flow...

在 AppDelegate.m 类中使用波纹管创建方法...

-(void)MethodName{//your method name
    YourViewController *objViewController = [[[YourViewController alloc] initWithNibName:@"YourViewController" bundle:nil] autorelease]; ///define your viewcontroller name like "FirstViewController" 
    UINavigationController *yourNavigationController = [[[UINavigationController alloc] initWithRootViewController:objViewController] autorelease];

    self.window.rootViewController = yourNavigationController;
}

When you want redirect on firstview just call this method from appdelegate object....

当您想在 firstview 上重定向时,只需从 appdelegate 对象调用此方法....

回答by MLBDG

For iOS6...

对于iOS6...

[self.view.window.rootViewController dismissViewControllerAnimated:YES completion:nil];