从子视图模型反向绑定到父视图模型



我有一个名为CustomizeMenuItemViewModel的主视图模型,它有一个属性maxItemCount,每当用户与视图交互以在购物袋中添加一个项目时,该属性需要减少到0。视图与它自己的名为CustomizationQuantityCellViewModel的视图模型交互,该模型是从主视图模型实例化的。我找不到一种方法来绑定属性maxItemCount到视图交互,其中CustomizationQuantityCellViewModel在两者之间。

相关代码:

final class CustomizeMenuItemViewModel {
private(set) var item: MenuItem
@Binding private(set) var maxUnitCount: Int
// MARK: - Initializer
init(item: MenuItem) {
self.item = item
_maxUnitCount = .constant(0)
}
func menuItem(option: Option, index: Int) -> MenuItemCellType {
let selections = Array(option.selections)
if option.multiselect {
if option.max > 0 {
maxUnitCount = option.max
return .quantity(CustomizationQuantityCellViewModel(selection: selections[index], maxUnit: $maxUnitCount), (index != selections.count - 1))
} else {
return .checkbox(CustomizationMenuItemCellViewModel(selection: selections[index]), (index != selections.count - 1))
}
} else {
if option.name.lowercased().contains("bun") {
return .card(CustomizationMenuItemCellViewModel(selection: selections[index]), (index != selections.count - 1))
} else if option.max > 0 {
maxUnitCount = option.max
return .quantity(CustomizationQuantityCellViewModel(selection: selections[index], maxUnit: $maxUnitCount), (index != selections.count - 1))
} else {
return .radiobutton(CustomizationMenuItemCellViewModel(selection: selections[index]), (index != selections.count - 1))
}
}
}
}
final class CustomizationQuantityCellViewModel: ObservableObject {
// MARK: - Properties
private(set) var selection: Selection
@Binding private(set) var maxUnit: Int
@Published private(set) var selectedUnits: Int
// MARK: - Initializer
init(selection: Selection, maxUnit: Binding<Int>) {
self.selection = selection
self._maxUnit = maxUnit
self.selectedUnits = 0
}
func increaseSelectedUnit() {
if maxUnit > 0 {
selectedUnits += 1
maxUnit -= 1
}
}
func decreseSelectedUnit() {
if selectedUnits > 0 {
selectedUnits -= 1
maxUnit += 1
}
}
}
struct CustomizationQuantityCell: View {
// MARK: - Properties
@ObservedObject private var viewModel: CustomizationQuantityCellViewModel
// MARK: - Initializer
init(viewModel: CustomizationQuantityCellViewModel) {
self.viewModel = viewModel
}
// MARK: - Body
var body: some View {
VStack {
HStack(spacing: 16) {
Text(viewModel.selection.name)
.font(.system(size: 16))
.foregroundColor(Color(Colors.brown500.color))
.padding(.top, 20)
.padding(.bottom, 20)
Spacer()
HStack(spacing: 32) {
Button {
viewModel.decreseSelectedUnit()
} label: {
Image(Images.minus.name)
}
.buttonStyle(.plain)
Text("(viewModel.selectedUnits)")
.font(.system(size: 15))
.foregroundColor(Color(Colors.brown500.color))
Button {
viewModel.increaseSelectedUnit()
} label: {
Image(Images.plus.name)
}
.buttonStyle(.plain)
}
}

}
}
}

我尝试了上面的方法,但绑定总是将0设置为maxUnitCount。

只需从maxUnitcount中删除绑定,并使该视图模型为ObservableObject。

final class CustomizeMenuItemViewModel: ObservableObject {
private(set) var item: MenuItem
@Published private(set) var maxUnitCount: Int

你需要改变的另一件事是你的另一个视图模型。会成功的。

final class CustomizationQuantityCellViewModel: ObservableObject {
// MARK: - Properties
private(set) var selection: Selection
private(set) var maxUnit: Int
private(set) var selectedUnits: Int
// MARK: - Initializer
init(selection: Selection, maxUnit: Int) {
self.selection = selection
self._maxUnit = maxUnit
self.selectedUnits = 0
}
}

相关内容

  • 没有找到相关文章

最新更新