发布的 Bool 变量自动重置

Published Bool variable resetting automatically

我有以下简单的 SwiftUI 视图,其中包含一个组件,其中包含带有复选框的营销偏好选项列表和一个更新偏好的按钮:

@StateObject var marketingPreferencesViewModel: MarketingPreferencesViewModel

var body: some View {
    VStack {
        MarketingPreferencesView(viewModel: .init())
        Button {
            marketingPreferencesViewModel.marketingUpdateRequested()
        } label: {
            Text("Update prefs")
        }
    }
}

MarketingPreferencesView如下:

struct MarketingPreferencesView: View {

@ObservedObject var viewModel: MarketingPreferencesViewModel

var body: some View {
    VStack(alignment: .leading) {
        marketingPreference(type: .email)
        marketingPreference(type: .directMail)
        marketingPreference(type: .notification)
        marketingPreference(type: .sms)
        marketingPreference(type: .telephone)
    }
}

func marketingPreference(type: MarketingOptions) -> some View {
    HStack {
        if viewModel.preferencesAreLoading {
            ProgressView()
        } else {
            Button {
                switch type {
                case .email:
                    viewModel.emailMarketingEnabled.toggle()
                case .notification:
                    viewModel.notificationMarketingEnabled.toggle()
                case .sms:
                    viewModel.smsMarketingEnabled.toggle()
                case .telephone:
                    viewModel.telephoneMarketingEnabled.toggle()
                case .directMail:
                    viewModel.directMailMarketingEnabled.toggle()
                }
                
            } label: {
                switch type {
                case .email:
                    viewModel.emailMarketingEnabled ? Image.General.Checkbox.checked : Image.General.Checkbox.unChecked
                // ... remaining switch cases follow same pattern
                }
            }
        }
        Text(type.title())
    }
    .padding(.bottom, Constants.bottomPadding)
}

}

以及 MarketingPreferencesViewModel:

 class MarketingPreferencesViewModel: ObservableObject {
    @Published var emailMarketingEnabled = false {
        didSet {
            print(emailMarketingEnabled)
        }
    }
    @Published var directMailMarketingEnabled = false
    @Published var notificationMarketingEnabled = false
    @Published var smsMarketingEnabled = false
    @Published var telephoneMarketingEnabled = false
    
    private func updateMarketingPreferences() {
        // For now, just printing emailMarketingEnabled value for debugging
        print(emailMarketingEnabled)
    }
    
    func marketingUpdateRequested() {
         self.updateMarketingPreferences()
       }  
    }
}

所以用户点击营销偏好按钮,这会切换视图模型中的相应值。

但是,出于某种原因,当我点击 marketingUpdateRequested() 方法时,如果我检查 emailMarketingEnabled 的值,即使我已经切换它,它也总是错误的。

重现步骤:

我已将 didSet 放在 emailMarketingEnabled 上 属性 以查看它是否在其他地方发生变化,但事实并非如此。那为什么我设置为true,点击按钮的时候是false呢?

您需要实例化 MarketingPreferencesViewModel,然后将其传递给您的 MarketingPreferencesView,如以下示例代码:

// -- here
@StateObject var marketingPreferencesViewModel = MarketingPreferencesViewModel()  

var body: some View {
    VStack {
        MarketingPreferencesView(viewModel: marketingPreferencesViewModel)  // <-- here
        Button {
            marketingPreferencesViewModel.marketingUpdateRequested()
        } label: {
            Text("Update prefs")
        }
    }
}