swiftui页面呈现后空白

时间:2019-10-29 23:28:02

标签: swiftui swiftui-list

我对视图有疑问。进入该视图后,将其渲染一会儿,然后消失。我从Firebase加载数据。在分层级别上,这是第三个视图

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

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

问题是onAppear返回空后的“ 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()
    }
}

LineupViewModel

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
}

0 个答案:

没有答案