在TabView中加载数据

时间:2019-11-18 21:36:23

标签: ios swiftui

我创建了一个具有4个视图的tabview。每个视图都有一个viewModel,我从ana api加载视图中所需的数据。除第一个视图始终为空,除非我在选项卡中点击然后再次在第一个视图上点击,否则它运行正常。

有什么主意,如何确保第一视图的所有数据在加载后就在那里?

在下面找到我的代码

谢谢

-Contentview

@ViewBuilder
    var body: some View {
        if isLoggedIn() {
            MainScreen()
        } else {
            UnAuthenticatedScreen()
        }
    }

-MainView

@ObservedObject var eventsVM: EventsVM = EventsVM()
    var body: some View {
            TabView(){
                HomeScreen(events: self.eventsVM.events)
                    .tabItem {
                        Image(systemName: "house")
                        Text("Home")
                }
                .navigationBarHidden(true)
                EventsScreen(events: self.eventsVM.events)
                    .tabItem {
                        Image(systemName: "calendar")
                        Text("Events")
                }.navigationBarHidden(true)
    }

}

-EventsVM

import Foundation
import Combine

class EventsVM: ObservableObject {

    let didChange = PassthroughSubject<[EventModel], Never>()
    private let eventsService: EventService

    @Published var events = [EventModel]()

    init() {
        self.eventsService = EventService()
        self.fetchEvents()
    }

    private func fetchEvents(){
        self.eventsService.getAllEvents { (_events, _error) in
            guard let events = _events else { return }
            self.events = events
        }
    }


}

主视图

import SwiftUI

struct HomeScreen: View {
    var eventsVM: EventsVM
    @State var news: [NewsModel] = []
    @State var albums = [AlbumModel]()

    init(eventsVM: EventsVM){
        self.eventsVM = EventsVM()
    }

    var body: some View {
        GeometryReader { gr in
            VStack(alignment: .leading, spacing: 0) {
                HStack{
                    Spacer()
                }
                HStack {
                    Spacer()
                    Text("About Us")
                    Image("logo_squad")
                        .resizable()
                        .frame(width: 50, height: 50)
                }
                Text("Events").font(Font.custom("Francois One", size: 30)).foregroundColor(.red)
                ScrollView(.horizontal, showsIndicators: false){
                    HStack {
                        ForEach(self.eventsVM.events, id: \.self) { event in
                            HomeEventRow(event: event).frame(width: gr.size.width - 60, height: 170)
                            .overlay(
                                RoundedRectangle(cornerRadius: 10)
                                    .stroke(Color.gray, lineWidth: 1).shadow(radius: -2)
                            )
                                .clipShape(RoundedRectangle(cornerRadius: 10))

                        }
                    }
                }
//                Text("News").font(Font.custom("Francois One", size: 30)).foregroundColor(.red)
//                ScrollView(.horizontal, showsIndicators: false){
//                    HStack {
//                        ForEach(self.news, id: \.self){ _news in
//                            HomeNewsRow(news: _news)
//                                .frame(width: gr.size.width - 60, height: 150)
//                            .overlay(
//                                RoundedRectangle(cornerRadius: 10)
//                                    .stroke(Color.gray, lineWidth: 1).shadow(radius: -2)
//                            )
//                                .clipShape(RoundedRectangle(cornerRadius: 10))
//                        }
//                    }
//                }
//                Text("Albums").font(Font.custom("Francois One", size: 30)).foregroundColor(.red)
//                ScrollView(Axis.Set.horizontal, showsIndicators: true){
//                    HStack {
//                        ForEach(self.albums, id: \.self){ album in
//                            HomeMediaRow(album: album)
//                                .frame(width: gr.size.width - 60, height: 150)
//                        }.overlay(
//                            RoundedRectangle(cornerRadius: 10)
//                                .stroke(Color.gray, lineWidth: 1).shadow(radius: -2)
//                        )
//                            .clipShape(RoundedRectangle(cornerRadius: 10))
//                    }
//                }
                Spacer()
            }
            .padding(EdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 0))
            .onAppear {
                print("self.eventsVM.events \(self.eventsVM.events)")
            }

        }.padding(EdgeInsets(top: 0, leading: 5, bottom: 0, trailing: 0))

    }
}

struct HomeScreen_Previews: PreviewProvider {
    static var previews: some View {
        HomeScreen(eventsVM: EventsVM())
    }
}

0 个答案:

没有答案