【问题标题】:Fatal error: No ObservableObject of type SelectedTab found. A View.environmentObject(_:) for SelectedTab may be missing as an ancestor of this view致命错误:未找到 SelectedTab 类型的 ObservableObject。 SelectedTab 的 View.environmentObject(_:) 作为此视图的祖先可能会丢失
【发布时间】:2021-07-07 11:01:53
【问题描述】:

出现错误 - 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()
            }
        }
        }
}

}

【问题讨论】:

  • 正如错误所说,需要将对象注入到TabBarContentView的祖先中的环境中
  • 已经完成 - struct TabBarContentView: View { @EnvironmentObject var tab: SelectedTab var body: some View { Home().environmentObject(tab) } }
  • 不,这是试图从环境中获取一个对象,但它不存在,所以你得到了错误。

标签: ios swiftui xcode12 observableobject environmentobject


【解决方案1】:

你有:

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

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

【讨论】:

  • 在 parentView 中添加后,仍然出现一些错误。能否请您连接并查看我的代码哪里有问题?
  • 向我们展示您开始和声明 SelectedTab 的代码,以及如何将其传递给 TabBarContentView,以及您遇到的错误。
【解决方案2】:

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.

更改家的签名。从TabBarContentView 传递selectedTab

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.

Home 中使用@ObservedObject 代替@EnvironmentObject

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 用于任何其他目的,请使用第二种方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-22
    • 2020-05-11
    • 2021-06-24
    • 2021-12-30
    • 2021-07-08
    • 2020-05-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多