【问题标题】:SwiftUI Fatal error: No ObservableObject of type " " found and @EnvironmentObject error: may be missing as an ancestor of this viewSwiftUI 致命错误:未找到“”类型的 ObservableObject 和 @EnvironmentObject 错误:可能作为此视图的祖先丢失
【发布时间】:2020-05-11 03:33:35
【问题描述】:

关于目标的详细信息:我想从 firebase 中获取数据,以问答的形式将数据列在行中。答案将有一个文本字段来更新它。有一个保存按钮,可以通过将问题和答案上传到 firebase 来保存答案。

预期与结果: 当点击导航到屏幕时,我的主菜单中应该有一个按钮。结果是我的应用程序冻结,并且在控制台中出现以下错误。

错误

Fatal error: No ObservableObject of type AllQuestionsAndAnswersClass found.
A View.environmentObject(_:) for AllQuestionsAndAnswersClass may be missing as an ancestor of this view.: file /BuildRoot/Library/Caches/com.apple.xbs/Sources/Monoceros/Monoceros-39.4.4/Core/EnvironmentObject.swift, line 55
2020-01-24 13:21:25.523326-0600 FirebaseStarterApp[5025:894100] Fatal error: No ObservableObject of type AllQuestionsAndAnswersClass found.
A View.environmentObject(_:) for AllQuestionsAndAnswersClass may be missing as an ancestor of this view.: file /BuildRoot/Library/Caches/com.apple.xbs/Sources/Monoceros/Monoceros-39.4.4/Core/EnvironmentObject.swift, line 55

我的想法 我不太确定从哪里开始,但我认为这与必须将环境对象添加到预览并在 App Delegate 中执行某些操作有关。我看过一些关于 Scene Delegate 的东西,但我只有 App Delegate,我不确定是否有区别

我的尝试 我试图查看以下链接,但它们对我的情况没有帮助SwiftUI @EnvironmentObject error: may be missing as an ancestor of this view

No ObservableObject of type found

这是我的 SwiftUI 屏幕代码

@available(iOS 13.0, *)
final class AllJobDataClass: ObservableObject  {
    @Published var allJobDataArray = [JobInfo]()
}

@available(iOS 13.0, *)
final class AllQuestionsAndAnswersClass: ObservableObject  {
    @Published var questionsAndAnswersInfoArray = [QuestionAndAnswer]()
}

@available(iOS 13.0.0, *)
struct SingleQuestionAndAnswerRow: View {

     @State var singleQuestionAndAnswerVar: QuestionAndAnswer

     var body: some View {

       return HStack{
             Text("\(singleQuestionAndAnswerVar.question)")

             TextField("Enter some text", text: $singleQuestionAndAnswerVar.answer)
                 .border(Color.black)
         }
     }
}


@available(iOS 13.0.0, *)
struct NewQuestionsScreenSwiftUIView: View {

    @EnvironmentObject var allJobDataVar: AllJobDataClass
    @EnvironmentObject var allQuestionsAndAnswerVar: AllQuestionsAndAnswersClass

    init()
    {
        fetchDataFromFirebase()
    }

    var body: some View {
        ScreenBackground
        {
            List(self.allQuestionsAndAnswerVar.questionsAndAnswersInfoArray) { questionAndAnswer in
                    //here is where questionAnswerRow is presented
                SingleQuestionAndAnswerRow(singleQuestionAndAnswerVar: questionAndAnswer)

                }
        }
        .onTapGesture {
            self.endEditingFunction()
        }
    }
}

@available(iOS 13.0.0, *)
struct NewQuestionsScreenSwiftUIView_Previews: PreviewProvider {
    static var previews: some View {
        NewQuestionsScreenSwiftUIView()
    }
}

我的App Delegate代码

import UIKit
import Firebase
import CoreData

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Window setup
        FirebaseApp.configure()
        window = UIWindow(frame: UIScreen.main.bounds)
        window?.rootViewController = UINavigationController(rootViewController: ATCClassicLandingScreenViewController(nibName: "ATCClassicLandingScreenViewController", bundle: nil))
        window?.makeKeyAndVisible()

        return true
    }
  }

【问题讨论】:

    标签: ios swift swiftui


    【解决方案1】:

    您将环境对象声明为

    struct NewQuestionsScreenSwiftUIView: View {
        @EnvironmentObject var allJobDataVar: AllJobDataClass
        @EnvironmentObject var allQuestionsAndAnswerVar: AllQuestionsAndAnswersClass
    

    但您必须在使用 NewQuestionsScreenSwiftUIView 之前将它们注入某处,例如在创建时(或通过父视图隐式)。在提供的代码中,您创建NewQuestionsScreenSwiftUIView() 的位置不可见,但在那个位置应该类似于

    NewQuestionsScreenSwiftUIView()
        .environmentObject(AllJobDataClass()) // < or instance if created before
        .environmentObject(AllQuestionsAndAnswersClass()) // < or instance if created before
    

    【讨论】:

    • 在几个将可观察对象设置到视图环境中的项目中,我仍然得到(例如):致命错误:未找到 MazeDocument 类型的 ObservableObject。 MazeDocument 的 View.environmentObject(_:) 作为此视图的祖先可能会丢失。:文件 SwiftUI,第 0 行。该视图是通过 NSDocument 中 makeWindowControllers() 覆盖中的祖先视图创建的;我确实检查了,如果我将 makeWindowcontrollers async 中的代码延迟了 0.25 秒,它没有帮助。有什么想法为什么即使在视图层次结构中调用 .environmentObject 也看不到它?
    • 我会的,但是我已经将代码重组为(成功)使用 ObservableObject 和 Published,所以我不容易发布示例。不过,感谢您的关注-我主要希望有一个已知的通用答案
    猜你喜欢
    • 2021-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-22
    • 2020-11-25
    • 2021-06-24
    • 1970-01-01
    相关资源
    最近更新 更多