11

Í每当我单击 NavLink 时,我都会尝试刷新此列表

NavigationView {
    List(feed.items.indices, id:\.self) { i in
        NavigationLink(destination: ListFeedItemDetail(idx: i).environmentObject(self.feed)) {
            ListFeedItem(item: self.$feed.items[i])
        }
    }
}

该列表由环境对象内的数组组成。问题:只有当我切换到另一个选项卡或关闭我之前使用模态视图的应用程序并且它在那里工作时才会刷新。(我用.onAppear 做到了)有什么想法吗?

例子

问题:当您点击列表中的项目并点击切换按钮时,EnvironmentObject 已更改,但此更改仅在我更改选项卡并再次更改回时反映

import SwiftUI
import Combine

struct TestView: View {

    @State var showSheet: Bool = false
    @EnvironmentObject var feed: TestObject
    func addObjects() {
        var strings = ["one","two","three","four","five","six"]
        for s in strings {
            var testItem = TestItem(text: s)
            self.feed.items.append(testItem)
        }
    }

    var body: some View {
        TabView {
            NavigationView {
                List(feed.items.indices, id:\.self) { i in
                    NavigationLink(destination: detailView(feed: self._feed, i: i)) {
                        HStack {
                            Text(self.feed.items[i].text)
                            Text("(\(self.feed.items[i].read.description))")
                        }
                    }
                }
            }
                .tabItem({ Text("Test") })
                .tag(0)

            Text("Blank")
                .tabItem({ Text("Test") })
                .tag(0)
        }.onAppear {
            self.addObjects()
        }
    }
}

struct detailView: View {
    @EnvironmentObject var feed: TestObject
    var i: Int
    var body: some View {
        VStack {
            Text(feed.items[i].text)
            Text(feed.items[i].read.description)
            Button(action: {  self.feed.items[self.i].isRead.toggle() }) {
                Text("Toggle read")
            }
        }
    }
}

final class TestItem: ObservableObject {
    init(text: String) {
        self.text = text
        self.isRead = false
    }
    static func == (lhs: TestItem, rhs: TestItem) -> Bool {
        lhs.text < rhs.text
    }
    var text: String
    var isRead: Bool

    let willChange = PassthroughSubject<TestItem, Never>()
    var read: Bool {
        set {
            self.isRead = newValue
        }
        get {
            self.isRead
        }
    }
}

class TestObject: ObservableObject {
    var willChange = PassthroughSubject<TestObject, Never>()

    @Published var items: [TestItem] = [] {
        didSet {
            willChange.send(self)
        }
    }
}
4

1 回答 1

12

我有一个类似的问题,这是我想出的黑客。

在您的“TestView”中声明:

@State var needRefresh: Bool = false

将此传递到您的“detailView”目的地,例如:

NavigationLink(destination: detailView(feed: self._feed, i: i, needRefresh: self.$needRefresh)) {
     HStack {
            Text(self.feed.items[i].text)
            Text("(\(self.feed.items[i].read.description))")
     }.accentColor(self.needRefresh ? .white : .black)
 }

请注意“.accentColor(self.needRefresh ? .white : .black)”以在“needRefresh”更改时强制刷新。

在您的“detailView”目的地添加:

@Binding var needRefresh: Bool

然后在您的 Button 操作中的“detailView”中,添加:

self.needRefresh.toggle()
于 2019-08-30T22:12:15.340 回答