从根视图弹出后推送UIViewController

3

我有一个使用storyboard和导航控制器的应用程序。在应用程序流程的某个阶段,我的视图堆栈上大约有四个视图,此时我必须弹出所有视图,直到回到根视图。然后我需要手动推入另一个视图。

我尝试了各种方法都没有成功。我尝试使用内置的API调用:

[self.navigationController popToRootViewControllerAnimated:YES];

在这里,我尝试通过引用根视图并调用转场方法来调用推送转场。

RootView *obj = [[RootView alloc] init];
[obj callSegue];

或者

[self.navigationController performSegueWithIdentifier:@"pushView" sender:self];

无论如何,我对这个问题完全感到困惑。有人可以帮忙吗?

更新:

谢谢大家的回复。我继续搜索并找到了一个解决方案,肯定不止一个。

// Reference to navigation controller. Apparently if you use self.navigationController in a popToRootViewController call it sets self.navigationController to nil.
UINavigationController *navigationController = self.navigationController;
[navigationController popToRootViewControllerAnimated:NO];

// Reference to view to push - must set storyboard ID in inspector
ViewToPush *viewRef = (ViewToPush *)[self.storyboard instantiateViewControllerWithIdentifier:@"gameView"];
[navigationController pushViewController:gameView animated:NO];

你在哪里调用 [self.navigationController performSegueWithIdentifier:@"pushView" sender:self]; - rdurand
3个回答

2

这个怎么样?

[self.navigationController setViewControllers:@[rootViewController, viewControllerTwo] animated:YES];

这个代码将您的堆栈设置为根控制器和一个新的控制器,并使用推送动画。如果您需要关于rootViewController的快速参考,可以使用[[self.navigationController viewControllers] objectAtIndex:0]


1
一个很好的解决方案是使用“Unwind Segue”。基本上,unwind segue 是一种带你返回推入控制器堆栈的segue,并在目标控制器中执行IBAction方法。你想要做的就是从当前控制器到根控制器创建一个unwind segue,然后在调用的方法中放置一个performSegueWithIdentifier:调用。
这里有一个关于 unwind segues 的教程:Tutorial

0

也许你可以尝试这个:

  • 在你的根视图控制器中设置一个标志(一个@property),比如shouldPushAutomatically
  • 在你调用[self.navigationController popToRootViewControllerAnimated:YES];的VC中,实现prepareForSegue:WithIdentifier:方法。在这个方法中,使用(MyVC*)segue.destinationViewController来访问你的根视图控制器,并将你的标志设置为YES
  • 在你的根视图控制器的viewDidLoad中,尝试调用你的push segue(实际上,你可能需要在viewDidAppear中调用它)。

不确定这是否有效,但这是我尝试使其工作的方式。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接