ios - 如何以编程方式展开到另一个 Storyboard中的 ViewController?

标签 ios swift3 uiviewcontroller xcode8 uistoryboard

这让我抓狂了将近 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

最佳答案

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/

相关文章:

ios - 使用未声明的类型 "ViewController"

ios - 使用导致内存泄漏的音频剪辑

ios - 如何将 setEditing 状态转换为编辑/完成按钮的切换模式

iOS 5 - 如何在一个窗口中组合不同类型的 View ?

ios - 为什么 self.splitViewController == nil?

ios - Swift:将 JSON 字符串移动到数组的简写方式

iOS 子类 UIView - 如何从 ViewController 调用 UIButton 方法?

ios - 如何让应用程序根据用户移动的距离更新服务器?

ios - 在 Swift 3 中读取 JSON 文件时出错

ios - 浏览 UIViewControllers/UIView — 代码和概念问题