无效的导航按钮

时间:2020-01-06 20:08:27

标签: swift swiftui

我正在尝试制作一个简单的Master-Detail-FileViewer应用程序。在上一个FileViewer视图中,我想要一个按钮,该按钮具有使该文件成为收藏夹的选项(每个文件都有一个“ id”字符串,该字符串附加到Environment对象)。当您偏爱它们时,此对象将显示在主视图中,以便用户快速访问,并链接到FileViewer视图。但是,当用户点击并转到那里时,该按钮处于非活动状态-您无法点击它,并且它从蓝色变为黑色。如果要从收藏夹中删除它们,则不能。 非常感谢您告诉我出了什么问题以及如何使按钮处于活动状态。没有显示错误,该应用程序也不会崩溃。就是行不通。 预先感谢!

文件是“ judgement”和“ secondary”,均具有id和title属性。第二张图片是有问题的。

Normal condition Problem Master View

import SwiftUI

struct ContentView: View {

@EnvironmentObject var favouriteList: FavouritesList

var body: some View {
    NavigationView {
        List {
            NavigationLink(destination: JudgementsView()) {
                Text("Judgements")
            }
            NavigationLink(destination: SecondaryView()) {
                Text("Secondary acts")
            }
            ScrollView(.horizontal, showsIndicators: false) {
                VStack {
                    if favouriteList.items.isEmpty {
                        Text("Nothing favoured")
                    } else {
                        ForEach(favouriteList.items, id: \.self) { id in
                            VStack {
                                HStack {
                                    ForEach(judgementsTAXraw.filter {
                                        $0.id == id
                                    }) { judgement in
                                        NavigationLink(destination: FileViewer(file: judgement.id)) {
                                            Text(judgement.title).padding()
                                        }

                                    }
                                }
                                HStack {
                                    ForEach(secondaryTAXraw.filter {
                                        $0.id == id
                                    }) { secondary in
                                        NavigationLink(destination: FileViewer(file: secondary.id)) {
                                            Text(secondary.title).padding()
                                        }

                                    }
                                }

                            }


                        }
                    }

                }
            }

        }
    .navigationBarTitle(Text("Test"))
    }

}
}

struct JudgementsView: View {
var body: some View {
    List(judgementsTAXraw, id: \.id) { judgement in
        NavigationLink(destination: FileViewer(file: judgement.id)) {
            Text(judgement.title)
        }

    }
}
}

struct SecondaryView: View {
var body: some View {
    List(secondaryTAXraw, id: \.id) { secondary in
        NavigationLink(destination: FileViewer(file: secondary.id)) {
            Text(secondary.title)
        }

    }
}
}



struct FileViewer: View {
var file: String

@State private var showCopySheet = false

@EnvironmentObject var favouriteList: FavouritesList

var body: some View {
    Text(file)
        .navigationBarTitle(Text(""), displayMode: .inline)
        .navigationBarItems(trailing:
        Button(action: {
            self.showCopySheet = true
        }) {
            Image(systemName: "doc.on.doc").frame(minWidth: 40)
        }.actionSheet(isPresented: $showCopySheet) {
            ActionSheet(title: Text("What do you want to do?"), buttons: [
                .destructive(Text("favText"), action: {
                    if let index = self.favouriteList.items.firstIndex(of: self.file) {
                        self.favouriteList.items.remove(at: index)

                    } else {
                        self.favouriteList.items.append(self.file)
                    }
                }),
                .cancel()
            ])
            }
    )
}
}

Aaa,对象是一个单独的文件:

import Foundation

class FavouritesList: ObservableObject {
@Published var items = [String]() {
    didSet {
        let encoder = JSONEncoder()

        if let encoded = try? encoder.encode(items) {
            UserDefaults.standard.set(encoded, forKey: "FavouredItems")
        }
    }
}

init() {
    if let items = UserDefaults.standard.data(forKey: "FavouredItems") {
        let decoder = JSONDecoder()

        if let decoded = try? decoder.decode([String].self, from: items) {
            self.items = decoded
            return
        }
    }

    self.items = []
}
}

0 个答案:

没有答案