gpt4 book ai didi

ios - 如果两个 View 相同,为什么 TabView 显示空白屏幕?

转载 作者:行者123 更新时间:2023-12-01 15:27:33 26 4
gpt4 key购买 nike

我的应用程序中有一个 TabView,用于显示四个屏幕。两者相似但具有不同的数据源。如果我单击第一个选项卡 (EventsScreen),然后单击第三个选项卡 (MyBagScreen),则一切正常。如果我单击第一个选项卡 (EventsScreen),然后单击第二个选项卡 (EventsScreen),它只会加载一个空白屏幕(我认为它只是加载背景)。它们是两个不同的对象,为什么这不起作用?

AppView.swift

struct AppView: View {
var body: some View {
TabView {
EventsScreen().environmentObject(EventsViewModel(repo: IncentivesRespository()))
.tabItem {
Text("Barter")
Image("icon_barter")
}
EventsScreen().environmentObject(EventsViewModel(repo: OpportunityRepository()))
.tabItem {
Text("Earn")
Image("icon_earn")
}
MyBagScreen().environmentObject(MyBagViewModel())
.tabItem{
Text("My Bag")
Image("icon_bag")
}
AccountScreen().environmentObject(AccountViewModel())
.tabItem {
Text("Account")
Image("icon_account")

}
}
.onAppear(perform: {
UITabBar.appearance().backgroundColor = .black
})
.navigationBarHidden(true)
}
}

事件屏幕
struct EventsScreen: View {

@EnvironmentObject
var viewModel: EventsViewModel

let calendarManager = RKManager(calendar: Calendar.current, minimumDate: Date(), maximumDate: Date().addingTimeInterval(60*60*24*90), mode: 1)

@State
var currentPage: Int = 0
@State
private var searchTerm : String = ""

init() {
UITableView.appearance().separatorStyle = .none
}

var body: some View {
GeometryReader { geometry in
List {
ZStack(alignment: .top) {
EventViewController(controllers: self.viewModel.featureEventControllers, currentPage: self.$currentPage)
VStack {
SearchBar(text: self.$searchTerm)
.padding(EdgeInsets.init(top: 16, leading: 16, bottom: 0, trailing: 16))
HStack {
Spacer()
Chip(text: "Dates", action: {
self.viewModel.showDateSelection.toggle()
})
.sheet(isPresented: self.$viewModel.showDateSelection, onDismiss: {
self.viewModel.onDatesSelected(startDate: self.calendarManager.startDate, endDate: self.calendarManager.endDate)
}, content: {
RKViewController(isPresented: self.$viewModel.showDateSelection, rkManager: self.calendarManager)
})
Chip(text:"Type", action: {
self.viewModel.showTypeSelection.toggle()
})
Chip(text: "Points", action: {
self.viewModel.showPointsSelection.toggle()
})
Spacer()
}
}
}.listRowInsets(EdgeInsets())
.frame(height: 600)
ForEach(self.viewModel.eventSections.indices) { id in
EventSectionView(eventSection: self.viewModel.eventSections[id])
}
.listRowBackground(Color.black)
}
.navigationBarTitle(Text("Events"), displayMode: .inline)
.navigationBarHidden(true)
if self.viewModel.showPointsSelection {
EventsPointsRangeBottomSheet(isOpen: self.$viewModel.showPointsSelection, selectedRange: self.$viewModel.selectedRange, range: 0.0...self.viewModel.maxPoints, action: {
self.viewModel.onPointsSelected()
})

}
if self.viewModel.showTypeSelection {
TypeBottomSheet(categories: self.viewModel.allCategories, isOpen: self.$viewModel.showTypeSelection, selectedCategoryUuids: self.$viewModel.selectedCategoryUuids, action: {
self.viewModel.onTypesSelected()
})
}

}
}
}

最佳答案

你的代码快照是不可测试的,所以只是一个想法——你必须自己测试它。

因此,假设您的第二个 View 的环境对象是正确的,请尝试使用 .id 使这些 View 真正独特。修饰符,如下

TabView {
EventsScreen().environmentObject(EventsViewModel(repo: IncentivesRespository()))
.id(1) // << here !!
.tabItem {
Text("Barter")
Image("icon_barter")
}
EventsScreen().environmentObject(EventsViewModel(repo: OpportunityRepository()))
.id(2) // << here !!
.tabItem {
Text("Earn")
Image("icon_earn")
}
...

注:我看到用法 .navigationBarHidden(true)AppView - 如果这意味着您要包含 TabView进入 NavigationView ,然后 不要那样做 - 你不会让它工作的。 TabView 被设计为 仅根 View .如果您需要导航,请将其放置在选项卡内。

关于ios - 如果两个 View 相同,为什么 TabView 显示空白屏幕?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60793465/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com