my@EnvironmentObject
を使用して、TeamData を別のユーザーで使用できるようにしていますが、うまく機能しません。ListのnavigationLinkで別のビューに行っても大丈夫です。同じビューへのボタンをタップすると、クラッシュしまし
た.何か間違っていると思います。誰かがこの問題を手伝ってくれますか? @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)")
})
}
}
I create a enviromentObject in this View
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))
}
}
}
The view I use the @EnviromentObject
import SwiftUI
struct GameDetailRecordView: View {
@EnvironmentObject var teamResult : TeamResult
var body: some View {
Text("ID:\(teamResult.groupID)Name:\(teamResult.groupName)")
}
}
Error message:
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