【发布时间】: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)
}
}
好消息是模态框会出现但立即关闭我看到打开效果然后关闭效果...
解决方案
- 全部在更新部分
- 重构 RN 代码 原来在这个新的之前还有一个打开的模式。在我打开新模式之前,旧模式没有关闭......所以我更快地关闭旧模式,然后我显示新模式,一切都很好
【问题讨论】:
标签: ios swift react-native react-native-modules