当ObservedObject更改时,在SwiftUI视图中不会调用onReceive

时间:2020-02-27 18:42:12

标签: ios swift mvvm swiftui combine

每当ObservedObject中的变量发生更改时,我都无法在SwiftUI视图中触发onReceive方法。

我尝试了两种方法:使用 @Publish 和使用 PassthroughSubject <>

这是ViewModel

class MenuViewModel: ObservableObject {

@Published var selectedItems = Set<UUID>()
@Published var currentFocusItem: UUID?

// Output
let newItemOnFocus = PassthroughSubject<(UUID?), Never>()

// This function gets called good :)
func tapOnMenuItem(_ item: MenuItem) {
    if selectedItems.contains(item.id) {
        //These changes should trigger the onReceive?
        currentFocusItem = item.id  
        newItemOnFocus.send(item.id)
    } else {
        selectedItems.insert(item.id)
        currentFocusItem = nil
        newItemOnFocus.send(nil)
    }
}
}
尝试捕获 @Published var currentFocusItem

中的更改时,

此处是视图

struct MenuView: View {

    @ObservedObject private var viewModel: MenuViewModel
    @State var showPicker = false
    @State private var menu: Menu = Menu.mockMenu()


    init(viewModel: MenuViewModel = MenuViewModel()) {
        self.viewModel = viewModel
    }

    var body: some View {
        VStack {
            List(menu.items, selection: $viewModel.selectedItems) { item in
                MenuItemView(item: item)
            }

            Divider()
            getBottomView(showPicker: showPicker)
        }

        .navigationBarTitle("Title")
        .navigationBarItems(trailing: Button(action: closeModal) {
            Image(systemName: "xmark")
        })
        .onReceive(viewModel.$currentFocusItem, perform: { itemUUID in
            self.showPicker = itemUUID != nil // <-- This only gets called at launch time
        })           
    }
}

视图,但尝试捕获 PassthroughSubject <>

.onReceive(viewModel.newItemOnFocus, perform: { itemUUID in
            self.showPicker = itemUUID != nil // <-- This never gets called
        })

----------编辑----------

添加MenuItemView,尽管总是调用viewModel.tapOnMenuItem,所以我不确定它是否非常有用

MenuItemView 在这里:

struct MenuItemView: View {

    var item: MenuItem
    @ObservedObject private var viewModel: MenuViewModel = MenuViewModel()
    @State private var isSelected = false

    var body: some View {
        HStack(spacing: 24) {
            Text(isSelected ? " 1 " : item.icon)
                .font(.largeTitle)
                .foregroundColor(.blue)
                .bold()
            VStack(alignment: .leading, spacing: 12) {
                Text(item.name)
                    .bold()
                Text(item.description)
                    .font(.callout)
            }
            Spacer()
            Text("\(item.points)\npoints")
                .multilineTextAlignment(.center)
        }
        .padding()
        .onTapGesture {
            self.isSelected = true
            self.viewModel.tapOnMenuItem(self.item). // <-- Here tapOnMenuItem gets called
        }
    }

    func quantityText(isItemSelected: Bool) -> String {
        return isItemSelected ? "1" : item.icon
    }
}

我在做什么错了?

1 个答案:

答案 0 :(得分:4)

好吧,这就是-您的MenuViewMenuItemView使用视图模型的不同实例

1)

struct MenuView: View {

    @ObservedObject private var viewModel: MenuViewModel
    @State var showPicker = false
    @State private var menu: Menu = Menu.mockMenu()


    init(viewModel: MenuViewModel = MenuViewModel()) { // 1st one created

2)

struct MenuItemView: View {

    var item: MenuItem
    @ObservedObject private var viewModel: MenuViewModel = MenuViewModel() // 2nd one

因此,您修改了一个实例,但订阅了另一个实例的更改。就是这样。

解决方案:通过.environmentObject或从MenuViewMenuItemView的参数传递视图模型。