如何在ios14.2(SwiftUI)中同时刷新详细信息视图和主视图

时间:2020-10-13 14:27:36

标签: swiftui ios14

嗨,我想刷新由主视图下的数据更改触发的详细信息视图
我的方法是设置一个组合侦听器,并从详细信息视图触发刷新
它可以在ios13上运行,但是我不能使其在ios14.2上运行,有什么解决方法吗?
这是我的主视图。

import SwiftUI

struct MainView: View {
    
    @ObservedObject var viewModel : MainViewModel
    
    init(){
        viewModel = MainViewModel.shared
    }

    var body: some View {
        NavigationView{
            List {
                ForEach(viewModel.games ,id:\.self) { game in
                    NavigationLink(destination: self.navDest(game: game)){
                        Text("\(game)")
                    }
                }
            }
        }
    }
    
    func navDest(game: Int) -> some View{
        print("games value:",game)
        return LazyView(DetailView(game: game ))
    }
}

111


主视图模型侦听来自详细视图的事件并更新值

import Foundation
import SwiftUI
import Combine


class MainViewModel : ObservableObject {

    @Published var games : [Int] = []
    static let shared = MainViewModel()
    private var tickets: [AnyCancellable] = []
    
    init(){
        for i in 0...5{
            games.append(i)
        }
        addObservor()
    }
    
    func addObservor(){
        NotificationCenter.default.publisher(for: .updateGame)
            .map{$0.object as! Int}
            .sink { [unowned self] (game) in
                self.updateGame(game: game)
            }.store(in: &tickets)
    }
    
    func updateGame(game:Int){
        print("updateView index:",game)
        self.games[game] = 9999
        print("after update",games)
    }
}



import SwiftUI

struct DetailView: View {
    
    @ObservedObject var viewModel : DetailViewModel
    
    init(game: Int) {
        print("init detail",game)
        viewModel = DetailViewModel(game:game)
    }
    
    var body: some View {
        VStack{
            Text("\(viewModel.game)")
            Button("update"){
                viewModel.sendUpdate()
            }
        }
    }
}


当我从局部视图单击更新时,它应同时刷新主视图和局部视图。
(流程为DetailViewModlel-> MainViewModlel-> Refresh MainView-> Refresh DetailView(当前正在显示)),但在iOS 14.2上不起作用

3

    import Foundation
import SwiftUI


class DetailViewModel : ObservableObject {

    var period : String = ""
    var game : Int = 0
    
//    init(period:String,game: Int){
//        self.period = period
        init(game: Int){
        self.game = game
    }

    func sendUpdate(){
        NotificationCenter.default.post(name: .updateGame, object:  game)
    }
}

extension Notification.Name {
    static let updateGame = Notification.Name("updateGame")
   
}
struct LazyView<Content: View>: View {
    let build: () -> Content
    init(_ build: @autoclosure @escaping () -> Content) {
        self.build = build
    }
    var body: Content {
        build()
    }
}

DetailView

import SwiftUI

struct DetailView: View {
    
    @ObservedObject var viewModel : DetailViewModel
    
    init(game: Int) {
        print("init detail",game)
        viewModel = DetailViewModel(game:game)
    }
    
    var body: some View {
        VStack{
            Text("\(viewModel.game)")
            Button("update"){
                viewModel.sendUpdate()
            }
        }
    }
}


我已经记录了调试消息,看来数据确实已更改,但视图未更改。

0 个答案:

没有答案