swiftui - 渲染后swiftui页面空白
问题描述
我的观点有问题。有问题的视图一旦进入其中,渲染屏幕片刻然后消失。我从 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
}
解决方案
推荐阅读
- cmake - ctest -R 使用的正则表达式语言的语法
- php - Laravel - 尝试将项目连接到数据库的问题
- r - R 中的变音符号未正确显示
- amazon-web-services - Terraform 计划命令失败
- python-3.x - mac中导入ortools
- java - 从嵌套哈希图中删除条目时java中的ConcurrentModificationException
- python - Python3 Windows 7 文件路径处理
- c++ - 我应该以什么顺序构建我的 C++ 静态库
- java - 如何使用休眠将日期设置为实体中的主键
- react-native - Have a Tab Act as a Drawer React Native