swift - 从不同的 Storyboard实例化 ViewController 时应用程序崩溃

标签 swift uiviewcontroller uistoryboard uistoryboardsegue nsexception

我正在使用 Swift 3(最新的 Xcode)编写

我正在控制用户是否登录(异步任务检查设备和数据库之间的参数)。

如果响应显示设备无效,我将显示登录屏幕。看起来像这样:

extension UIViewController {

    func forceLogin() {

        let storyboard = UIStoryboard(name: "Login", bundle: nil)
        let controller = storyboard.instantiateViewController(withIdentifier: "LoginViewController")
        let navController = UINavigationController(rootViewController: controller)

        self.present(navController, animated: true, completion: nil)
    }  
}

登录 Storyboard看起来就像这样。

Storyboard example

当用户成功登录时,该功能启动:

DispatchQueue.main.async {
    self.dismiss(animated: true, completion: nil)
}

解雇可能发生在第一个和第二个 ViewController 中。

想象一下情况:

  1. 用户在关闭第二个 VC 后登录并返回主应用程序
  2. 设备正在从数据库中删除
  3. 检查后,用户需要重新登录
  4. forceLogin() 和...

libc++abi.dylib: terminating with uncaught exception of type NSException

当使用self.present(navController,animated:true,completion:nil)时会发生这种情况。

我有一些可能导致崩溃的想法,但我不确定:

  1. 在代码中而不是在 Storyboard 中创建导航 Controller
  2. 关闭还不够 - Storyboard以某种方式保留在内存中并且无法再次实例化

什么可能导致问题以及如何避免崩溃?

如果需要更多信息,请询问。

最佳答案

我总是忘记这一点。请记住像这样放置您的界面任务:

DispatchQueue.main.async {
   self.present(navController, animated: true, completion: nil)
}

关于swift - 从不同的 Storyboard实例化 ViewController 时应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45736473/

相关文章:

ios - 通过 Pods 安装的 FBSDKs - Swift 上没有这样的模块

ios - 从另一个 UIViewController 调用方法没有可见效果

ios - Split View Controller 更新 TableView

ios - NSLayoutConstraints 的 UIViewPropertyAnimator 使 View 消失

javascript - Parse 对象 ID 是否跨类唯一?

ios - '一个 AVPlayerItem 不能与多个 AVPlayer 实例相关联'

ios - UIViewController 内的 UICollectionView 不允许滚动

ios - initWithCoder 在 ios 中显示错误的帧

ios - 模型在 Storyboard场景中粘在目标上

ios - 使用选定的 TableView 项和选项卡栏 Controller 显示 View Controller