OGeek|极客世界-中国程序员成长平台

标题: ios - 如何以编程方式展开到另一个 Storyboard中的 ViewController? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 15:39
标题: ios - 如何以编程方式展开到另一个 Storyboard中的 ViewController?

这让我抓狂了将近 2 天。我已经尝试了几乎所有的方法,但无法得到任何结果。每一篇关于展开的帖子,似乎都没有专门解决展开回到 ViewController 的问题,在不同的 Storyboard中,更不用说以编程方式进行了。我做错了什么????

我有两个 ViewControllers 位于不同的 Storyboard中...

我想从位于我的注册 Storyboard中的SignupViewController放松到位于我的登录 Storyboard中的LoginViewController>。我想在用户成功注册后以编程方式执行展开,收到带有“确定”按钮的确认警报,然后单击“确定”按钮。

这是我想放松的地方,从 => 到

enter image description here

这就是我尝试做的方式

所以我在我的 LoginViewController.swift 文件中创建了一个 unwindToLoginViewController 操作方法...

enter image description here

然后像这样在我的 SignupViewController.swift 文件中创建对它的引用...

enter image description here

随后,我按预期获得了引用...

enter image description here

并将标识符设置为相同的,这样我就可以在我的代码中引用它...

enter image description here

当我运行我的应用程序并成功注册用户时,我会显示带有“确定”按钮的确认警报。当用户单击“确定”按钮时,我想放松到我的 LoginViewController,但什么也没有发生...

有什么线索吗???????

enter image description here



Best Answer-推荐答案


Unwind segues 应该只用于回退到已经呈现并且仍在 View Controller 层次结构中的 View Controller 。

在上面的评论中,很明显情况并非如此。看起来您的意图是关闭 View 层次结构中当前的所有内容,并将其替换为完全不同的场景(示例中的登录场景)。在这种情况下,您应该只用类似的东西替换 Root View Controller (如 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
})

但让我们暂时假设您确实想要使用展开转场,因为您认为 View Controller 在 View 层次结构中。 (我知道这里不是这种情况,但为了 future 的读者,我将探讨这种情况。)

底线,如果展开 segue 失败,您应该首先确认具有展开 IBAction 的 View Controller 确实在 View Controller 层次结构中。

如果您不确定 View Controller 层次结构,请暂停应用程序的执行并打印层次结构:

(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>

只需确保具有展开操作的 View Controller 显示在层次结构中即可。

最重要的是,您只能展开到 View Controller 层次结构中的当前内容,因此请确保它在那里。

关于ios - 如何以编程方式展开到另一个 Storyboard中的 ViewController?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46492813/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4