0

此处出错 - selectedTab = tab.getTab()

致命错误:未找到 SelectedTab 类型的 ObservableObject。SelectedTab 的 View.environmentObject(_:) 作为该视图的祖先可能会丢失。

也试过这个 - https://www.hackingwithswift.com/forums/swiftui/fatal-error-no-observableobject-of-type-order-found/3208

    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()
            }
        }
        }
}

}

4

2 回答 2

0

初始化Home期间tab不可用。

struct Home: View {
    @EnvironmentObject var tab: SelectedTab
    @State var selectedTab = "Home"
    
    init() {
        UITabBar.appearance().isHidden = true
        selectedTab = tab.getTab()  // tab is not available here.
    }

    ------
}


有两种方法可以解决此问题。

1.

更改家的签名。从. selectedTab_TabBarContentView

struct Home: View {
    @EnvironmentObject var tab: SelectedTab
    
    @State var selectedTab = "Home"
    
    init(selectedTab: String) {
        UITabBar.appearance().isHidden = true
        self.selectedTab = selectedTab
    }

    ---
 }
struct TabBarContentView: View {
    @EnvironmentObject var tab: SelectedTab
    
    var body: some View {
        Home(selectedTab: tab.getTab()).environmentObject(tab)
    }
}

2.

使用@ObservedObject而不是@EnvironmentObjectin Home

struct Home: View {
    @ObservedObject var tab: SelectedTab
    
    @State var selectedTab = "Home"
    
    init(tab: SelectedTab) {
        UITabBar.appearance().isHidden = true
        self.tab = tab
        self.selectedTab = tab.getTab()
    }
}

struct TabBarContentView: View {
    @EnvironmentObject var tab: SelectedTab
    
    var body: some View {
        Home(tab: tab)
    }
}


如果您不需要SelectedTab任何其他目的,请使用第二种方法。

于 2021-07-07T13:00:05.857 回答
0

你有:

struct Home: View {
@EnvironmentObject var tab: SelectedTab   // <--- here
....

TabBarContentView 也是如此。

这意味着您在父视图(例如应用程序)中有如下内容:

import SwiftUI

@main
struct MyApp: App {
    @StateObject var tab = SelectedTab()   // <--- here
    var body: some Scene {
        WindowGroup {
            Home().environmentObject(tab)  // <--- here
        }
    }
}  

如果你没有这样的东西,那么你会看到你看到的错误。

于 2021-07-07T12:09:27.980 回答