ios 如何返回到已经加载的 UIViewController?

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

How to segue back to a UIViewController that's already loaded?

objective-ciosstoryboardsegue

提问by TijuanaKez

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.

我在情节提要上有一个视图,它有一个按钮来执行某个操作。但是,要执行此操作,用户必须登录。按钮处理程序测试用户是否已登录,如果为 YES,则执行一个 segue,如果为 NO,则执行另一个 segue。NO segues 推送到登录视图控制器。还有另一个连接回第一个视图控制器的segue,所以如果登录成功,用户可以从他们离开的地方继续。视图控制器嵌入在导航控制器中。

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.

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

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

我怎样才能回到视图控制器的原始实例?

回答by Mick MacCallum

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.

假设您使用 " push" 表示您正在使用 a UINavigationController,那么您可以使用以下内容返回到堆栈顶部。

[self.navigationController popToRootViewControllerAnimated:YES];

or

或者

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

to pop to a specific level where <n>is the level.

弹出到特定级别,其中<n>是级别。

or

或者

[self.navigationController popViewControllerAnimated:YES];

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

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

回答by TRVD1707

Instead of using a segue to go back to the previous controller you should use the event that was associated with the segue going back to instead dismiss the current view controller (if segue was modal) or pop this from the navigation controller (if the segue was push) and the previous controller will show automatically.

与其使用 segue 返回前一个控制器,您应该使用与返回的 segue 相关联的事件来关闭当前视图控制器(如果 segue 是模态的)或从导航控制器弹出它(如果 segue 是推),之前的控制器将自动显示。

For example, if the modal segue that goes back was being performed when you pressed a button on your login screen, you delete the segue from the storyboard and on the touch up inside event of the button you link with your action that, upon successful login will call:

例如,如果当您在登录屏幕上按下按钮时正在执行返回的模态转场,您可以从故事板中删除转场,并在与您的操作链接的按钮的内部事件中删除该转场,在成功登录后将会通知:

 [self dismissViewControllerAnimated:YES completion:nil];

On the storyboard, you will right click on the button and drag the touch up inside sent event to the first responder of the view controller scene. This will execute the reverse transition that was executed when you performed the segue transition. You can use a modal segue for that, no need to use a navigation controller.

在情节提要上,您将右键单击该按钮并将内部发送事件的触摸向上拖动到视图控制器场景的第一响应者。这将执行在您执行 segue 转换时执行的反向转换。您可以为此使用模态转场,无需使用导航控制器。

If you pushed the login view onto the navigation controller, then you have to pop it from the stack (if going back to the previous view). Use one of these:

如果您将登录视图推送到导航控制器上,那么您必须从堆栈中弹出它(如果返回到前一个视图)。使用其中之一:

    [[self navigationController] popViewControllerAnimated:YES];  // goes back to previous view
[[self navigationController] popToViewController: myViewControllerAfterLogin animated:YES]; // goes back to specific view on stack. 
[[self navigationController] popToRootViewControllerAnimated:YES]; // goes back to first view on the stack

The animated transition should reverse the type of transition used on the segue, so curl up will be curled down when you dismiss the view.

动画过渡应该与转场上使用的过渡类型相反,因此当您关闭视图时,向上卷曲将向下卷曲。

回答by malhal

If you are using a segue to go forward then you should use an unwind segue to go back. Detailed info on how to use an unwind segue is in this Technical Note TN2298 Using Unwind Segues.

如果您使用 segue 前进,那么您应该使用 unwind segue 返回。有关如何使用展开转场的详细信息,请参阅技术说明 TN2298 使用展开转场

回答by Tim

malhal is right - it's worth clicking on his link.

马尔哈尔是对的 - 值得点击他的链接。

The code has seems to have moved on a little from Apple's example and is even more flexible. I have three scenes but only one custom ViewController .m file. I did not want a navigation stack. Access to the subsequent scenes is by segues from buttons. I added this line in the base view controller .m file:

代码似乎与 Apple 的示例有所不同,并且更加灵活。我有三个场景,但只有一个自定义 ViewController .m 文件。我不想要导航堆栈。访问后续场景是通过按钮的segues。我在基本视图控制器 .m 文件中添加了这一行:

- (IBAction)unwindForSegue:(UIStoryboardSegue *)unwindSegue towardsViewController:(UIViewController *)subsequentVC{}

Then in the third scene I control-dragged from a button to the exit icon and selected the unwind action. It now drops back from scene 3 back to scene 2 without needing to call a dismissViewController method.

然后在第三个场景中,我从一个按钮控制拖动到退出图标并选择了放松动作。它现在从场景 3 返回到场景 2,而无需调用dismissViewController 方法。