具有@EnvironmentObject的导航栏项目

时间:2020-02-17 15:21:26

标签: ios swift swiftui

我想使用SwiftUI创建一个Settings视图。我主要以苹果公司提供的有关SwiftUI的官方示例来实现我的代码。设置视图中应该有一个开关,可以显示是否显示我的收藏夹项目。

现在我有一个地标列表和一个设置视图。

struct Landmark: Hashable, Codable, Identifiable {
    var id: Int
    var imageName: String
    var title: String
    var isFavorite: Bool
    var description: String

    enum CodingKeys: String, CodingKey {
        case id, imageName, title, description
    }
}
final class UserData: ObservableObject {
    @Published var showFavoriteOnly: Bool = false
    @Published var items: [Landmark] = landmarkData
    @Published var showProfile: Bool = false
}
struct ItemList: View {
    @EnvironmentObject var userData: UserData
    @State var trailing: Bool = false

    init() {
        UITableView.appearance().separatorStyle = .none
    }

    var body: some View {
        NavigationView {
            List {
                VStack {
                    CircleBadgeView(text: String(landmarkData.count), thickness: 2)
                    Text("Tutorials available")
                }.frame(minWidth:0, maxWidth: .infinity)

                ForEach(userData.items) { landmark in
                    if !self.userData.showFavoriteOnly || landmark.isFavorite {
                        ZStack {
                            Image(landmark.imageName)
                                .resizable()
                                .frame(minWidth: 0, maxWidth: .infinity)
                                .cornerRadius(10)
                                .overlay(ImageOverlay(text: landmark.title), alignment: .bottomTrailing)
                            Text(String(landmark.isFavorite))


                            NavigationLink(destination: TutorialDetailView(landmark: landmark)) {
                                EmptyView()
                            }.buttonStyle(PlainButtonStyle())
                        }
                    }
                }
            }.navigationBarTitle("Tutorials")
                .navigationBarItems(trailing: trailingItem())
        }
    }
}

extension ItemList {
    func trailingItem () -> some View {
        return HStack {

            if userData.showProfile {
                NavigationLink(destination: ProfileView()) {
                    Image(systemName: "person.circle")
                        .imageScale(.large)
                        .accessibility(label: Text("Profile"))
                }
            }

            NavigationLink(destination: SettingsView().environmentObject(userData)) {
                Image(systemName: "gear")
                    .imageScale(.large)
                    .accessibility(label: Text("Settings"))
            }
        }
    }
}

如您所见,可以从我的NavigationView的SettingsView访问我的navigationBarItems。我不知道这是不是问题,但是当我将Toggle放在ListView内时,它可以正常工作。但是现在,当我触发切换以仅启用收藏夹时,我的应用程序立即崩溃。

我尝试从Show profile触发SettingsView切换,并且可以正常工作。

struct SettingsView: View {
    @EnvironmentObject var userData: UserData

    var body: some View {
        Form {
            Section(header: Text("General")) {
                Toggle(isOn: $userData.showProfile) {
                    Text("Show profile")
                }
                Toggle(isOn: $userData.showFavoriteOnly) {
                    Text("Favorites only")
                }
            }

            Section(header: Text("UI")) {
                Toggle(isOn: .constant(false)) {
                    Text("Dark mode")
                }

                NavigationLink(destination: Text("third")) {
                    Text("Third navigation")
                }
            }

        }.navigationBarTitle(Text("Settings"), displayMode: .inline)
    }
}

简而言之,当我触发SettingsView切换时,崩溃出现在我的Show only favorite中,然后我尝试返回上一个视图ItemListView

我唯一可获得的关于错误的信息是Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)

您可以在我的GitHub上找到整个项目:https://github.com/Hurobaki/swiftui-tutorial

一些帮助将不胜感激:)

1 个答案:

答案 0 :(得分:1)

这是示例代码的最低版本,可以正常工作:

struct Landmark: Hashable, Codable, Identifiable {
  var id: Int
  var imageName: String
  var title: String
  var isFavorite: Bool
  var description: String
}

final class UserData: ObservableObject {
  @Published var showFavoriteOnly: Bool = false
  @Published var items: [Landmark] = [
    Landmark(id: 1, imageName: "a", title: "a", isFavorite: true, description: "A"),
    Landmark(id: 2, imageName: "b", title: "b", isFavorite: false, description: "B")
  ]
}

struct ContentView: View {
  @EnvironmentObject var userData: UserData

  var body: some View {
    NavigationView {
      List(userData.items.filter { !userData.showFavoriteOnly || $0.isFavorite }) { landmark in
        Text(String(landmark.isFavorite))
      }
      .navigationBarTitle("Tutorials")
      .navigationBarItems(trailing: trailingItem())
    }
  }

  func trailingItem () -> some View {
    return HStack {
      NavigationLink(destination: SettingsView()) {
        Text("Settings")
      }
    }
  }
}

struct SettingsView: View {
  @EnvironmentObject var userData: UserData

  var body: some View {
    Form {
      Section(header: Text("General")) {
        Toggle(isOn: $userData.showFavoriteOnly) {
          Text("Favorites only")
        }
      }
    }.navigationBarTitle(Text("Settings"), displayMode: .inline)
  }
}