gpt4 book ai didi

SwiftUI在PresentationMode上随机崩溃?.wrappedValue.dismiss()

转载 作者:行者123 更新时间:2023-12-03 09:18:24 30 4
gpt4 key购买 nike

这就是崩溃的样子
enter image description here
所以它随机崩溃在UIKit行上

UIKitCore
-[UIViewController _ancestorViewControllerOfClass:allowModalParent:] + 44
我在默认的SwiftUI导航堆栈中有View:
struct MyView: View {
@EnvironmentObject var viewModel: MyViewModel
@Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>

var body: some View {
ZStack {
......
}
.onAppear {
self.viewModel
.onViewAppear(presentationMode: self.presentationMode)
}
}
}

final class MyViewModel {
var presentationMode: Binding<PresentationMode>?

func onViewAppear(presentationMode: Binding<PresentationMode>) {
self.presentationMode = presentationMode
}

func hide() {
presentationMode?.wrappedValue.dismiss() // crashes after calling this
}
}
因此,我以这种方式将MyView插入导航堆栈:
NavigationLink(
destination: MyView()
) {
Image(systemName: "plus.circle")
.font(.title)
}
然后,在几秒钟后用户按下MyView中的按钮后,我在MyViewModel中调用 hide()。它几乎一直都在工作,但在5-10%的情况下会崩溃。

最佳答案

对我来说,解决的方法是设置.navigationViewStyle(StackNavigationViewStyle())

NavigationView { content }.navigationViewStyle(StackNavigationViewStyle())

关于SwiftUI在PresentationMode上随机崩溃?.wrappedValue.dismiss(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62969859/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com