Swift UI - 切换不隐藏文本元素

Swift UI - Toggle Not Hiding Text Elements

我有一个简单的应用程序,它有两个标签页——首选项页面和目录页面。目录页面显示姓名、地址和其他有关人员的基本信息(模拟数据),首选项页面显示切换,这些切换应确定应显示有关人员的哪些字段。

禁用切换不会隐藏目录页面上显示的禁用属性(例如,名称、地址),甚至会在控制台中记录警告:invalid mode 'kCFRunLoopCommonModes' provided to CFRunLoopRunSpecific - break on _CFRunLoopError_RunCalledWithInvalidMode to debug. This message will only appear once per execution.

ContentView(应用程序的根视图-指向两个页面)

struct ContentView: View {
    var body: some View {
        TabView {
            DirectoryView()
                .tabItem {
                    Text("Directory")
                    Image(systemName: "list.bullet.rectangle.portrait")
                }
            PreferencesView()
                .tabItem {
                    Text("Preferences")
                    Image(systemName: "gear")
                }
        }
        .environmentObject(PreferencesModel())
    }
}

DirectoryView(显示人员信息)

struct DirectoryView : View {
    @EnvironmentObject var preferencesModel:PreferencesModel
    let personModel = PersonModel()
    
    var body: some View {
        ScrollView {
            ForEach(personModel.people) { person in
                VStack(alignment: .leading) {
                    if preferencesModel.displayName {
                        HStack {
                            Text("Name: ")
                                .font(.body)
                                .fontWeight(.bold)
                            Text(person.name)
                        }
                        .frame(width: UIScreen.main.bounds.size.width, height: 20, alignment: .leading)
                    }
                    if preferencesModel.displayAddress {
                        HStack {
                            Text("Address: ")
                                .font(.body)
                                .fontWeight(.bold)
                            Text(person.address)
                        }
                        .frame(width: UIScreen.main.bounds.size.width, height: 20, alignment: .leading)
                    }
                    if preferencesModel.displayCompany {
                        HStack {
                            Text("Company: ")
                                .font(.body)
                                .fontWeight(.bold)
                            Text(person.company)
                        }
                        .frame(width: UIScreen.main.bounds.size.width, height: 20, alignment: .leading)
                    }
                    if preferencesModel.displayExperience {
                        HStack {
                            Text("Years of Experience: ")
                                .font(.body)
                                .fontWeight(.bold)
                            Text(String(person.yearsOfExperience))
                        }
                        .frame(width: UIScreen.main.bounds.size.width, height: 20, alignment: .leading)
                    }
                }
                .padding()
                Divider()
            }
        }
    }
}

PreferencesModel(ViewModel 存储要显示哪些字段的首选项)

class PreferencesModel: ObservableObject {
    var displayName = true
    var displayAddress = true
    var displayCompany = true
    var displayExperience = true
}

PreferencesView(使用开关查看)

struct PreferencesView: View {
    @EnvironmentObject var preferencesModel:PreferencesModel
    
    var body: some View {
        ScrollView {
            VStack {
                Toggle("Name", isOn: $preferencesModel.displayName)
                Toggle("Address", isOn: $preferencesModel.displayAddress)
                Toggle("Company", isOn: $preferencesModel.displayCompany)
                Toggle("Years of experience", isOn: $preferencesModel.displayExperience)
            }
        }
    }
}

您缺少 @Published ObservableObject 个变量。您还应该将 PreferenceModel 放在 ContentView 中的 @StateObject 中,以保护它免受 re-creating.

的影响