简体   繁体   English

如何以编程方式展开到另一个情节提要中的ViewController?

[英]How to unwind to a ViewController in another storyboard programmatically?

This has been driving me nuts for almost 2 days now. 这已经让我发疯了近2天了。 I've tried just about everything, and can't get any results. 我已经尝试了几乎所有内容,但没有任何结果。 Every post that addresses unwinding, doesn't seem to specifically address unwinding back to a ViewController in a different storyboard , let alone doing it programmatically. 解决发布问题的每个帖子似乎都没有专门解决发布到不同故事板上的ViewController的问题,更不用说以编程方式进行了。 What am I doing wrong????? 我究竟做错了什么?????

I have two ViewControllers that reside in different storyboards ... 我有两个位于不同情节提要中的ViewControllers ...

I want to unwind from my SignupViewController located in my Signup storyboard to my LoginViewController located in my Login storyboard . 我想从位于Signup故事板上的 SignupViewController到位于Login故事板上的 LoginViewController放松。 I want to perform the unwind programmatically after the user successfully registers, receives a confirmation alert with an OK button, and clicks the OK button. 我想在用户成功注册以编程方式执行展开操作,收到带有“确定”按钮的确认警报,然后单击“确定”按钮。

This is where I want to unwind from => to 这是我要从=>放松的地方

在此处输入图片说明

This is how I am attempting to do it 这就是我试图做到的

So I create an unwindToLoginViewController action method in my LoginViewController.swift file like so... 所以我像这样在我的LoginViewController.swift文件中创建一个unwindToLoginViewController动作方法...

在此处输入图片说明

And create a reference to it in my SignupViewController.swift file like so... 像这样在我的SignupViewController.swift文件中创建对它的引用...

在此处输入图片说明

Subsequently, I get the reference as expected... 随后,我得到了预期的参考...

在此处输入图片说明

And set the Identifier in the same, so I can reference it in my code... 并设置相同的标识符 ,以便我可以在我的代码中引用它...

在此处输入图片说明

When I run my app, successfully register a user, I display a confirmation alert with an OK button. 当我运行我的应用程序时,成功注册用户时,我将显示一个确认警报,并带有“确定”按钮。 When the user clicks the OK button, I want to unwind to my LoginViewController , but NOTHING happens... 当用户单击“确定”按钮时,我想展开到我的LoginViewController ,但是什么都没有发生……

Any clues????????? 任何线索?????????

在此处输入图片说明

Unwind segues should only be used to unwind back to a view controller that already has been presented and is still in the view controller hierarchy. Unwind segue仅应用于退回到已经显示的视图控制器,并且仍然在视图控制器层次结构中。

In comments, above, it's clear that this is not the case. 在上面的评论中,很明显并非如此。 It would appear that your intent is to dismiss everything currently in the view hierarchy and replace it with a completely different scene (a login scene in your example). 看来您的意图是消除视图层次结构中当前的所有内容,并用一个完全不同的场景(在您的示例中为登录场景)替换它。 In that case, you should just replace the root view controller with something like (as shown in https://stackoverflow.com/a/41144822/1271826 ): 在这种情况下,您应该只将根视图控制器替换为以下内容(如https://stackoverflow.com/a/41144822/1271826所示):

let window = UIApplication.shared.keyWindow!
let frame = window.rootViewController!.view.frame

let controller = UIStoryboard(name: "Login", bundle: nil)
    .instantiateViewController(withIdentifier: "...")!

controller.view.frame = frame

UIView.transition(with: window, duration: 0.3, options: .transitionCrossDissolve, animations: {
    window.rootViewController = controller
}, completion: { completed in
    // maybe do something here
})

But let's assume for a second that you did want to use an unwind segue because you thought the view controller would be in the view hierarchy. 但是,让我们假设您确实想使用展开序列,因为您认为视图控制器位于视图层次结构中。 (I know that's not the case here, but for the sake of future readers, I'll going to explore this scenario.) (我知道这里不是这种情况,但是为了将来的读者,我将探讨这种情况。)

Bottom line, if an unwind segue ever fails, you should first confirm that the view controller with the unwind IBAction actually is in the view controller hierarchy. 最重要的是,如果展开任务失败,则应首先确认具有展开IBAction的视图控制器实际上在视图控制器层次结构中。

If you're ever unsure about the view controller hierarchy, pause execution of your app and print the hierarchy: 如果您不确定视图控制器的层次结构,请暂停应用程序的执行并打印层次结构:

(lldb) expr -l objc++ -O -- [UIViewController _printHierarchy] 
<UINavigationController 0x7ff341047800>, state: appeared, view: <UILayoutContainerView 0x7ff340f15610>
   | <MyApp.FirstViewController 0x7ff340c1d2f0>, state: disappeared, view: <UIView 0x7ff340c336a0> not in the window
   | <MyApp.SecondViewController 0x7ff340d08880>, state: disappeared, view: <UIView 0x7ff340d09620> not in the window
   | <MyApp.ThirdViewController 0x7ff3433089d0>, state: disappeared, view: <UIView 0x7ff343104b80> not in the window
   | <MyApp.FourthViewController 0x7ff343102810>, state: disappeared, view: <UIView 0x7ff340d0b150> not in the window
   | <MyApp.FifthViewController 0x7ff340f19c60>, state: appeared, view: <UIView 0x7ff340d02010>

Just make sure that the view controller with the unwind action is shown in the hierarchy. 只要确保在层次结构中显示了具有展开动作的视图控制器即可。

Bottom line, you can only unwind to something currently in the view controller hierarchy, so make sure it's there. 最重要的是,您只能展开到视图控制器层次结构中当前存在的内容,因此请确保已存在。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何以编程方式从一个情节提要中的一个视图控制器转到另一个情节提要中的另一个视图控制器? - How to go from one view controller in One storyboard to another viewcontroller in another storyboard programmatically? 如何以编程方式在新的故事板中选择到新的ViewController - How to Programmatically Segue to a New ViewController in a New Storyboard 在Storyboard中以编程方式呈现ViewController - presenting ViewController Programmatically in Storyboard 不使用StoryBoard展开到其他viewController - unwind to other viewController without using StoryBoard 以编程方式导航到另一个故事板上的navigationcontroller和tabbarcontroller内的viewcontroller - Navigate programmatically to viewcontroller inside navigationcontroller & tabbarcontroller on another storyboard 将Modal UINavigationController展开到另一个ViewController - Unwind a Modal UINavigationController to another ViewController 如何将一个Storyboard的ContainerView链接到另一个Storyboard中的ViewController? - How to link ContainerView of one Storyboard to ViewController in another Storyboard? 使用ViewController以编程方式连接Storyboard - Programmatically connecting Storyboard w/ ViewController 如何在 ios siwft 中以编程方式将 storyboard 视图控制器加载到标签栏 controller 中? - How to load storyboard viewcontroller into programmatically tabbar controller in ios siwft? 展开ViewController - Unwind ViewController
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM