此处出错 - selectedTab = tab.getTab()
致命错误:未找到 SelectedTab 类型的 ObservableObject。SelectedTab 的 View.environmentObject(_:) 作为该视图的祖先可能会丢失。
class SelectedTab: ObservableObject {
@Published var tab:String = "Home"
func setTab(tabName: String) {
tab = tabName
}
func getTab() -> String {
return tab
}
}
struct TabBarContentView: View {
@EnvironmentObject var tab: SelectedTab
var body: some View {
Home().environmentObject(tab)
}
}
struct TabBarContentView_Previews: PreviewProvider {
static var previews: some View {
Group {
TabBarContentView()
.previewDevice(PreviewDevice(rawValue: "iPhone 12 Pro Max"))
.previewDisplayName("iPhone 12 Pro Max")
}
}
}
struct Home: View {
@EnvironmentObject var tab: SelectedTab
@State var selectedTab = "Home"
init() {
UITabBar.appearance().isHidden = true
selectedTab = tab.getTab()
}
var body: some View{
NavigationView() {
ZStack(alignment: .bottom, content: {
TabView(selection: $selectedTab){
HomeTab()
NewsTab()
ProfileTab()
MoreTab()
}
}
}
}
}