gpt4 book ai didi

swiftui - @EnvironmentObject 通过 navigationLink 不能很好地工作

转载 作者:行者123 更新时间:2023-12-03 16:37:40 30 4
gpt4 key购买 nike

我用我的 @EnvironmentObject让我的 TeamData 可以被不同的人使用,但效果不佳。当我通过列表中的导航链接转到另一个 View 时,这很好。当我点击同一 View 的按钮时,它崩溃了..
我想我说错了什么。有人可以帮助解决这个问题吗?这是否意味着我需要添加 @EnvironmentObject首先在 NavigationView 以便我可以使用它?谢谢

演示:https://youtu.be/-iRadrHd94c

import SwiftUI

struct GameRecordListView: View {
@EnvironmentObject var teamResult : TeamResult

@State var hint : String = ""
@State var successRegistration : Bool = false
@State var sheetPresented: Bool = false
var body: some View {
VStack{

List(0 ..< 12){ item in
NavigationLink(destination: GameDetailRecordView()){ <-this works well
Text("444")

}
}
VStack{
Spacer()
HStack{
Spacer()
NavigationLink(destination: GameDetailRecordView()) <-this doesn't works well and crashed
{
Image(systemName: "pencil.circle")
.resizable()
.frame(width: 35, height: 35)
.padding(12)
.background(Color.blue)
.foregroundColor(Color.white)
.clipShape(Circle())

}
}.padding(15)

}

}.onAppear(perform: {
print("teamResult:\(self.teamResult.groupID):\(self.teamResult.groupName)")
})
}
}

我在这个 View 中创建了一个 environmentObject
import SwiftUI
import SwiftyJSON

struct TeamDetail: View {
@EnvironmentObject var teamResult : TeamResult
// @ObservedObject var teamResult: TeamResult
var roles = ["GameRecord", "Schedule", "Money", "Member"]
@State var show = false
@State private var selectedIndex = 0
var body: some View {
ZStack{
VStack {
Picker(selection: $selectedIndex, label: Text("")) {
ForEach(0..<roles.count) { (index) in
Text(self.roles[index])
}
}
.pickerStyle(SegmentedPickerStyle())

HStack{
containedView()

}
Spacer()

}


}.navigationBarTitle(teamResult.groupName)
.onAppear(perform:{

})
}



//select different view by selectedIndex
func containedView() -> AnyView {
switch selectedIndex {
case 0:
return AnyView(
GameRecordListView().environmentObject(teamResult) <-create environmentObject here
)

case 1:

return AnyView(Text("")
.padding(30))


case 2:
return AnyView(
BookkeepingView().environmentObject(teamResult)

)


default:
return AnyView(
TeamMemberListView().environmentObject(teamResult))
}

}
}

我使用@EnviromentObject 的 View
import SwiftUI

struct GameDetailRecordView: View {
@EnvironmentObject var teamResult : TeamResult
var body: some View {
Text("ID:\(teamResult.groupID)Name:\(teamResult.groupName)")
}
}

错误信息:

Fatal error: No ObservableObject of type TeamResult found.
A View.environmentObject(_:) for TeamResult may be missing as an ancestor of this view.: file /BuildRoot/Library/Caches/com.apple.xbs/Sources/Monoceros_Sim/Monoceros-39.4.3/Core/EnvironmentObject.swift, line 55
2020-01-20 01:14:58.466655+0800 Fitness(SwiftUI)[49035:4851345] Fatal error: No ObservableObject of type TeamResult found.
A View.environmentObject(_:) for TeamResult may be missing as an ancestor of this view.: file /BuildRoot/Library/Caches/com.apple.xbs/Sources/Monoceros_Sim/Monoceros-39.4.3/Core/EnvironmentObject.swift, line 55

最佳答案

尝试在 NavigationLink 的 View 上添加 environmentObject

 NavigationLink(destination: GameDetailRecordView().environmentObject(teamResult))

关于swiftui - @EnvironmentObject 通过 navigationLink 不能很好地工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59812640/

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