如何切换回已经加载的UIViewController? [英] How to segue back to a UIViewController that's already loaded?

查看:141
本文介绍了如何切换回已经加载的UIViewController?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在故事板上有一个视图,它有一个按钮来执行某个动作。但是,要执行此操作,用户必须登录。按钮处理程序将测试用户是否已登录并执行一个segue(如果是),另一个则执行NO。 NO segues推送到登录视图控制器。还有另一个segue连接回第一个视图控制器,因此如果登录成功,用户可以从中断的地方继续。视图控制器嵌入在导航控制器中。

I have a view on a storyboard the has a button to perform a certain action. To perform this action though, the user must be logged in. The button handler tests if the user is logged in and performs one segue if YES and another if NO. The NO segues pushes to a login view controller. There is another segue connecting back to the first view controller so if login is successfull the user can pick up where they left off. The view controllers are embedded in a navigation controller.

问题是,'return'segue正在加载一个全新的视图控制器实例,而不是引用原始实例所以我最终在内存中使用空接口元素和该视图控制器的2个副本。

Problem is, the 'return' segue is loading a whole new instance of the view controller and not referencing the origional instance so I end up with empty interface elements and 2 copies of that view controller in memory.

如何返回到视图控制器的原始实例?

How can I get back to the original instance of the view controller?

推荐答案

假设您使用 push ,则表示您使用的是 UINavigationController ,然后您可以使用以下内容返回到堆栈顶部。

Assuming by your use of "push" you mean that you are using a UINavigationController, then you can use the following to return to the top of the stack.

[self.navigationController popToRootViewControllerAnimated:YES];

UIViewController *prevVC = [self.navigationController.viewControllers objectAtIndex:<n>];
[self.navigationController popToViewController:prevVC animated:YES];

弹出到< n> 是等级。

[self.navigationController popViewControllerAnimated:YES];

如果您只想弹出一个级别备份导航堆栈。

If you just want to pop one level back up the navigation stack.

这篇关于如何切换回已经加载的UIViewController?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆