我试图从 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 代码原来在这个新的之前打开了另一个 modal。在我打开新模式之前,旧模式没有关闭......所以我更快地关闭旧模式,然后我显示新模式,一切都很好