1

我的观点有问题。有问题的视图一旦进入其中,渲染屏幕片刻然后消失。我从 firebase 加载数据。在层次级别,它是第三个视图

视图 A -> 视图 B -> 视图 C

如果从 B 到达 C,我有问题,如果从 A 到达,则问题不存在。

问题是 onAppear return empty 后的“self.lineup.fetchHomeTeam” ViewB 传递给 ViewC 的数据是正确的

VIEW C (TeamsModuleView) -> 有问题的页面

struct TeamsModuleView: View {

    @ObservedObject var lineup = LineupViewModel()
    @EnvironmentObject var settings: UserSettings

    var body: some View {
        ScrollView(.vertical) {
            Group {
                VStack(spacing: 20, content:  {
                    ForEach(lineup.lineupHome, id: \.self) { module in
                        HStack(alignment: .top, spacing: 10, content: {
                            ForEach(module.name, id: \.self) { name in
                                Group {
                                    Spacer()
                                    VStack(alignment: .center, spacing: 0, content: {
                                        Spacer().frame(height: 20)
                                        Image("home")
                                            .resizable()
                                            .frame(width: 30, height: 30)
                                        Text(name)
                                            .foregroundColor(Color.white)
                                            .font(.system(size: 10))
                                            .frame(maxWidth: .infinity,  alignment: .center)
                                            .multilineTextAlignment(.center)
                                        Spacer().frame(height: 5)
                                    })
                                    Spacer()
                                }
                            }
                        })
                    }
                    ForEach(lineup.lineupAway, id: \.self) { module in
                        HStack(alignment: .top, spacing: 10, content: {
                            ForEach(module.name, id: \.self) { name in
                                Group {
                                    Spacer()
                                    VStack(alignment: .center, spacing: 0, content: {
                                        Spacer().frame(height: 5)
                                        Image("transfert")
                                            .resizable()
                                            .frame(width: 30, height: 30)
                                        Text(name)
                                            .foregroundColor(Color.white)
                                            .font(.system(size: 10))
                                            .frame(maxWidth: .infinity,  alignment: .center)
                                            .multilineTextAlignment(.center)
                                        Spacer().frame(height: 20)
                                    })
                                    Spacer()
                                }
                            }
                        })
                    }                        
                })
                .background(
                    Image("field3")
                        .resizable()
                        .aspectRatio(contentMode: .fill)
                ).edgesIgnoringSafeArea(.all)
            }
        }.onAppear  {
            self.lineup.fetchHomeTeam(fixturesId: String(self.settings.fixtureId), teamId: String(self.settings.teamHomeId), team: self.settings.teamHome)
            self.lineup.fetchAwayTeam(fixturesId: String(self.settings.fixtureId), teamId: String(self.settings.teamAwayId), team: self.settings.teamAway)
        }.onDisappear {
            print(self.lineup.lineupHome.isEmpty)
        }
        .navigationBarTitle("Formazione", displayMode: .inline) //Return true i dont why
    }
}

struct TeamsModuleView_Previews: PreviewProvider {

    static var previews: some View {        
        TeamsModuleView()
    }
}

阵容视图模型

final class LineupViewModel: ObservableObject {

    @Published var lineup = Lineup()
    @Published var lineupHome = [LineupView]()
    @Published var lineupAway = [LineupView]()


    func fetchHomeTeam(fixturesId: String, teamId: String, team: String) {
        Webservices().getLineUp(fixturesId: fixturesId, teamId: teamId, team: team) {
            self.lineup = $0
            var lineupModTemp = [LineupView]()
            -
            -
            -
            DispatchQueue.main.async {

                self.lineupHome = lineupModTemp
            }
        }
    }

    func fetchAwayTeam(fixturesId: String, teamId: String, team: String) {
        Webservices().getLineUp(fixturesId: fixturesId, teamId: teamId, team: team) {
            self.lineup = $0
            var lineupModTemp = [LineupView]()
            -
            -
            -
            DispatchQueue.main.async {
              self.lineupAway = lineupModTemp
            }
        }
    }
}

UserSettings(真实数据在View B onclik中修改)

class UserSettings: ObservableObject {
    @Published var teamHomeId = 505
    @Published  var teamAwayId = 518
    @Published  var teamHome = "Brescia"
    @Published  var teamAway = "Inter"
    @Published  var fixtureId = 232614
}
4

0 回答 0