@EnvironmentObject无法通过NavigationLink正常运行

时间:2020-01-19 17:34:51

标签: swiftui swiftui-navigationlink

我使用自己的@EnvironmentObject来让TeamData可以不同地使用,但效果不佳。当我通过List中的navigationLink转到另一个视图时,就可以了。当我在同一个视图上点击一个按钮时,它崩溃了。 我想我说错了。有人可以帮忙解决这个问题吗?是否意味着我需要先在NavigationView中添加@EnvironmentObject才能使用它?谢谢

演示: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)")
     })
}
}

我在此视图中创建了一个enviromentObject

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

1 个答案:

答案 0 :(得分:4)

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

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