9

我使用 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)")
     })
}
}

我在这个视图中创建了一个环境对象

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 的视图

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
4

1 回答 1

6

尝试在 NavigationLink 中的视图上添加 environmentObject

 NavigationLink(destination: GameDetailRecordView().environmentObject(teamResult))
于 2020-01-19T18:29:51.447 回答