有关目标的详细信息:我想从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()
}
}
我的应用代理
代码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
}
}
答案 0 :(得分:3)
您将环境对象声明为
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