【问题标题】:Modul should show modal/UIViewController in main thread but it doesnt work without delayModul 应该在主线程中显示 modal/UIViewController 但它不能立即工作
【发布时间】:2022-01-07 05:40:19
【问题描述】:

我试图通过 Swift 模块在我的 React 本机应用程序中呈现 UIViewController 我是这样介绍的

  let screen = UIApplication.shared.windows.filter {$0.isKeyWindow}.first
  screen?.rootViewController?.present(payController!, animated: true, completion: nil);
  

我得到这个错误: UIApplication.windows must be used from main thread only

好的,我必须将它添加到我的线程中

  DispatchQueue.main.async {
    let screen = UIApplication.shared.windows.filter {$0.isKeyWindow}.first
    screen?.rootViewController?.present(payController!, animated: true, completion: nil);
  }

当我以小延迟调用此函数时它工作正常

setTimeout(() => {
  showMyWiew();
}, 500);

或像这样延迟到swift

  DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
    let screen = UIApplication.shared.windows.filter {$0.isKeyWindow}.first
    screen?.rootViewController?.present(payController!, animated: true, completion: nil);
  }

但如果我删除延迟,则不会显示此模式。但它应该在那里。我在 swift 的登录中看到了这一点,这证实了我的理论:

[Presentation] Attempt to present <PKAddPaymentPassViewController: 0x103d6b2e0> on <UIViewController: 0x103c25480> (from <UIViewController: 0x103c25480>) which is already presenting <RCTModalHostViewController: 0x10e21df40>.

PKAddPaymentPassViewController 是我的 UIViewController

我不知道如何解决这个问题...

更新

根据第一条评论我这样做了

  DispatchQueue.main.async {
    let screen = UIApplication.shared.windows.filter {$0.isKeyWindow}.first
    let controller = screen?.rootViewController?.presentedViewController;
    if controller == nil {
      screen?.rootViewController?.present(payController!, animated: true, completion: nil);
    } else {
      screen?.rootViewController?.presentedViewController?.present(payController!, animated: true, completion: nil)
    }

  }

好消息是模态框会出现但立即关闭我看到打开效果然后关闭效果...

解决方案

  1. 全部在更新部分
  2. 重构 RN 代码 原来在这个新的之前还有一个打开的模式。在我打开新模式之前,旧模式没有关闭......所以我更快地关闭旧模式,然后我显示新模式,一切都很好

【问题讨论】:

    标签: ios swift react-native react-native-modules


    【解决方案1】:

    那是因为在那一刻screen?.rootViewController 似乎已经呈现了另一个 ViewController (RCTModalHostViewController)。已经呈现一些 VC 的 VC不能呈现另一个 VC。您可以做的是在此时由 rootViewController 呈现的 VC 上调用 present:screen?.rootViewController.presentedViewController?.present(...)。 (在呈现的 ViewController 上呈现确实有效。在呈现另一个 VC 的 VC 上呈现则不行。)

    如果您这样做,您还应确保实际设置了 presentedViewController 而不是 nil。如果它是 nil,您可以像之前一样在 rootViewController 上致电出席。

    【讨论】:

    • 谢谢...好消息是模态将显示但立即关闭我看到打开效果然后关闭效果...检查我的更新...你能帮我吗也有问题?
    • 那么这可能取决于您要呈现的实际情况。在我看来,当您致电 present 时,presentedViewController 即将被解雇。当presentedViewController 被关闭时,它也会关闭它依次呈现的任何内容。所以这可以解释你最初的问题和新的问题。解决此问题的两种方法:1) 在代码中找到更好的位置来调用 present 或 2) 确保此时 presentedViewController 不会被解雇。我不知道你在这里对 react native 有多大影响......
    • 我试图将它添加到另一个地方但不起作用......现在我正在玩`controller?.isModalInPresentation = true这项工作......但我不能在模态中使用取消按钮...... . 知道为什么吗?
    • 如果时间问题是您使用 react native 的模块的限制,我认为您必须寻找一种完全不同的方法来获取视图。也许不使用模态?
    • 经过一些重构,它可以工作......谢谢
    猜你喜欢
    • 2019-06-16
    • 2013-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多