的View.environmentObject(_ :)作为此视图的祖先可能会丢失

时间:2020-03-28 08:34:04

标签: swiftui

我刚刚更新到Xcode 11.4,它破坏了我的代码。我将一些用户设置存储在ObservableObject中,如下所示:

class UserSettings: ObservableObject {  
    @Published var cardOrder = UserDefaults.standard.integer(forKey: "Card Order")  
    @Published var cardTheme = UserDefaults.standard.integer(forKey: "Card Theme")  
    @Published var translation = UserDefaults.standard.integer(forKey: "Translation")  
    @Published var overdueFirst = UserDefaults.standard.bool(forKey: "Overdue First")  
    @Published var randomNum = 0  
}  

这是我的主菜单,设置环境对象已成功向下传递到“设置”视图,我可以在其中保存和检索用户选择。

struct ContentView: View {  
  @State var settings = UserSettings()  

  var body: some View {  
    SubView().environmentObject(settings)  
  }  
}  

struct SubView: View {  

  @EnvironmentObject var settings: UserSettings  

  var body: some View {  
    List {  
      NavigationLink (destination: Flashcard()){  
        HStack {  
          Image(systemName: "rectangle.on.rectangle.angled")  
          Text(verbatim: "Study")      
        }  
      }  

      NavigationLink (destination: Settings()) {  
        HStack {  
          Image(systemName: "gear")  
          Text(verbatim: "Settings")  
        }  
      }  
    }  
  }  
}  

但是在我的抽认卡视图中,我得到一个错误:致命错误:未找到UserSettings类型的ObservableObject。作为该视图的祖先,可能缺少用于用户设置的View.environmentObject(_ :) 。:文件SwiftUI,第0行

错误出现在第13行中,我在其中启动Frontside。在原始代码中,我只是调用了Frontside子视图,但是我想解决这个错误,我必须添加.environmentObject(settings),但是即使添加了它,我的应用程序仍然可以编译,但是当我进入Flashcard视图时就会崩溃。 >

struct Flashcard: View {  
  @EnvironmentObject var settings: UserSettings  

  @State var colour = UserDefaults.standard.integer(forKey: "Card Theme") * 6  
  @State private var showResults: Bool = false  
  @State private var fullRotation: Bool = false  
  @State private var showNextCard: Bool = false  


  var body: some View {  
    let zstack = ZStack {  

      Frontside(id: $settings.randomNum, sheet: $showingSheet, rotate: $fullRotation, invis: $showNextCard, col: $colour).environmentObject(self.settings)  
      //   


      Backside(id: $settings.randomNum, sheet: $showingSheet, bookmark: $bookmarked, results: $showResults, rotate: $fullRotation, invis: $showNextCard, col: $colour, trans: $translation).environmentObject(self.settings)  
    //  

    }  
  }  

有人知道我在做什么错吗?该代码在先前的Xcode中已编译并运行良好。

5 个答案:

答案 0 :(得分:1)

我认为您也应该将settings对象传递给FlashCardSettings。 试试这个:

struct ContentView: View {  
  @State var settings = UserSettings()  

  var body: some View {  
    SubView().environmentObject(settings)  
  }  
}  

struct SubView: View {  

  @EnvironmentObject var settings: UserSettings  

  var body: some View {  
    List {  
      NavigationLink (destination: Flashcard().environmentObject(settings)){  
        HStack {  
          Image(systemName: "rectangle.on.rectangle.angled")  
          Text(verbatim: "Study")      
        }  
      }  

      NavigationLink (destination: Settings().environmentObject(settings)) {  
        HStack {  
          Image(systemName: "gear")  
          Text(verbatim: "Settings")  
        }  
      }  
    }  
  }  
}  

答案 1 :(得分:0)

我在模拟器中运行 iOS 14.3,就我而言,错误是关于我的 environmentObject NavigationController。通过在 ContentView() 中使用 .environmentObject(NavigationController()) 修改 SceneDelegate 解决了这个问题,如果您希望预览工作,也可以在 ContentView_Previews 中使用。

答案 2 :(得分:0)

@EnvironmentObject 必须直接用 @StateObject@ObservedObjectObservableObject 填充,而不是 @State

struct ContentView: View {  
    //@ObservedObject
    @StateObject var settings = UserSettings()  

    var body: some View {  
        SubView().environmentObject(settings)  
    }  
}  

注意:UserSettings 必须是 ObservableObject

Apple documentation on managing model data

struct BookReader: App {
    @StateObject var library = Library()

    var body: some Scene {
        WindowGroup {
            LibraryView()
                .environmentObject(library)
        }
    }
}

struct LibraryView: View {
    @EnvironmentObject var library: Library

    // ...
}

答案 3 :(得分:0)

import SwiftUI

@main

// there is a file with the name of your "projectApp" (JuegosSwiftUIApp in my case)

struct JuegosSwiftUIApp: App { 
    var body: some Scene {
        WindowGroup {
            DatosIniciales() // any view
                .environmentObject(Datos()) // this solved it (Datos() is class type Observableobject) 
        }
    }
}

答案 4 :(得分:-1)

删除let zstack =

ZStack {  

      Frontside(id: $settings.randomNum, sheet: $showingSheet, rotate: $fullRotation, invis: $showNextCard, col: $colour).environmentObject(self.settings)  
      //   


      Backside(id: $settings.randomNum, sheet: $showingSheet, bookmark: $bookmarked, results: $showResults, rotate: $fullRotation, invis: $showNextCard, col: $colour, trans: $translation).environmentObject(self.settings)  
    //  

    }