0

所以这是我的应用程序的简化版本,我尝试过重现错误,我有一个带有 2 个选项卡的 TabView(HomeView &OrderView),如果我使用 viewModel 放置选择索引,每次我去 OrderView 并返回 HomeView, HomeView 中加载的视图将消失,这对于其他选项卡中的所有视图和任何其他视图都是一样的。

调试日志中没有任何错误日志,API 获取完美,如果我打印它们,数据就在那里,变量也都很好,但是 UI 只在第一次加载时出现(在切换选项卡之前),然后它就消失了在我切换标签后。

如果我删除选项卡选择索引或使用@State 将索引放置在 ContentView 中,一切正常,但我需要将索引放在 ViewModel 中,以便我可以从另一个视图修改索引。

这种行为有什么问题?非常感谢您提前

内容视图.swift

struct ContentView: View {
    @Environment(\.managedObjectContext) private var viewContext
    @ObservedObject var viewModel = HomeViewModel()

    var body: some View {
        NavigationView {
            TabView(selection: self.$viewModel.homeTabSelected) {
                HomeView(homeVm: self.viewModel).tabItem {
                    VStack {
                        Image(systemName: "house")
                        Text("Home")
                    }
                }.tag(0)
                
                OrderView().tabItem {
                    VStack {
                        Image(systemName: "note.text")
                        Text("Order")
                    }
                }.tag(1)
            }
        }
    }
}

HomeViewModel.swift

class HomeViewModel: ObservableObject {
    @Published var homeTabSelected = 0
    
    func changeTab() {
        self.homeTabSelected = 2
    }
}

HomeView.swift

struct HomeView: View {
    @ObservedObject var homeVm: HomeViewModel
    
    var body: some View {
        ZStack {
            VStack {
                ScrollView {
                    VStack(alignment: .leading) {
                        ZStack(alignment: .top) {
                            
                            VStack(alignment: .leading) {
                                HStack {
                                    Text("Good Afternoon")
                                        .foregroundColor(Color.white)
                                }
                                
                                Spacer().frame(height: 25)
                                
                            }.offset(y: 50)
                            .frame(maxWidth: .infinity, alignment: .topLeading)
                            .padding(.horizontal, 20)
                            
                        }
                        
                        Group {
                            MenuListView(homeVm: self.homeVm)
                        }
                    }
                }
            }.edgesIgnoringSafeArea(.top)
            
        }.navigationBarHidden(true)
        .background(Color("c_fff"))
    }
}

MenuListView.swift

struct MenuListView: View {
    @ObservedObject var viewModel = MenuListViewModel()
    @ObservedObject var homeVm: HomeViewModel
    
    var body: some View {
        VStack {
            
            Group {
                VStack(alignment: .leading) {
                    ScrollView(.horizontal, showsIndicators: false) {
                        HStack {
                            ForEach(self.viewModel.category, id: \.self) { cat in
                                Button(action: {
                                }) {
                                    Text(cat.categoryTitle.capitalized)
                                        .padding(.horizontal, 15)
                                        .padding(.vertical, 8)

                                    // This will not appear after I changed tab and return to HomeView
                            }
                        }
                    }
                }
            }
        }
        .onAppear {
            self.viewModel.getMenuList()
        }
    }
}

MenuListViewModel.swift

import Combine
import SwiftUI

class MenuListViewModel: ObservableObject, ModelService {
    var apiSession: APIService
    @Published var menu = [MenuListItem]()
    @Published var category = [CategoryListItem]()
    @Published var promo = [HomePromo]()
    @Published var categorySelected = "promo"
    
    var cancellables = Set<AnyCancellable>()
    
    init(apiSession: APIService = APISession()) {
        self.apiSession = apiSession
    }
    
    func getMenuList() {
        let cancellable = self.getMenuList()
            .sink(receiveCompletion: { result in
                switch result {
                case .failure(let error):
                    print("Handle error menu: \(error)")
                case .finished:
                    break
                }
                
            }) { (menu) in
                self.menu = menu.products
                self.category = menu.category
                self.promo = menu.promos
        }
        cancellables.insert(cancellable)
    }
    
    
}

前

后

所以我有截图,在第一个应用程序启动时,数据被加载并正确加载到视图中,然后如果我点击第二个选项卡,然后返回第一个选项卡,视图就消失了(加载的数据和所有变量在日志中正确,也没有任何错误日志)

4

0 回答 0