xcode 无法使用navigationLink点击图像

vuv7lop3  于 2023-04-13  发布在  其他
关注(0)|答案(1)|浏览(153)
struct ContentView: View {
@State var recipes: [Recipe] = []

var body: some View {
    NavigationView {
        
        
        ScrollView {
            LazyVGrid(columns:  [GridItem(.flexible()), GridItem(.flexible())], spacing: 10) {
                ForEach(recipes) { recipe in
                    NavigationLink(destination: RecipeDetailView()) {
                        Image(recipe.coverPhoto)
                            .resizable()
                            .scaledToFill()
                            .frame(width: 200, height: 200)
                            .cornerRadius(0)
                            .overlay(
                                Text(recipe.name)
                                    .font(.custom("Bodoni 72 Book", size: 20))
                                    .foregroundColor(.white)
                                    .shadow(color: .black, radius: 0, x: 1, y: 1)
                                    .padding(.bottom, 10)
                                ,
                                alignment: .bottom
                            )
                            .padding(.bottom,0.5)
                        
                        
                    }
                    
                }
            }
        }
        .navigationTitle("Recipes")
        .onAppear {
            do {
                if let fileURL = Bundle.main.url(forResource: "recipes", withExtension: "json") {
                    let data = try Data(contentsOf: fileURL)
                    let decoder = JSONDecoder()
                    decoder.keyDecodingStrategy = .convertFromSnakeCase
                    self.recipes = try decoder.decode([Recipe].self, from: data)
                } else {
                    print("JSON file not found")
                }
            } catch {
                print(error.localizedDescription)
            }
        }
    }
}

}
我正在使用SwiftUI,为什么我不能用这段代码点击图像到达目的地?什么也没发生,没有错误。
我从json文件中获取细节,它在屏幕上完美地显示,有图像和标题。但似乎找不到为什么当我点击它们时什么也没有发生。
用真实的设备,模拟器试过。我试过列表。我用LazyVGrid做过,但也试过没有它。
编辑:

struct Recipe: Codable, Identifiable, Hashable {
let name: String
let instructions: String
let ingredients: [String]
let garnish: String
let coverPhoto: String
var id: UUID {
    return UUID()
}

}

7lrncoxx

7lrncoxx1#

问题在Recipe内:SwiftUI正在重新渲染视图,但保留recipes的状态。在每次重新渲染时,视图都会为每个Recipe获取id。由于id是计算属性,因此每次调用时都会返回新的UUID。因此List无法识别正确的配方,NavigationLink无法按预期工作。
要解决这个问题,您需要更改Recipe

struct Recipe: Codable, Identifiable, Hashable {
    let name: String
    let instructions: String
    let ingredients: [String]
    let garnish: String
    let coverPhoto: String
    let id: UUID

    init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        self.name = try container.decode(String.self, forKey: .name)
        self.instructions = try container.decode(String.self, forKey: .instructions)
        self.ingredients = try container.decode([String].self, forKey: .ingredients)
        self.garnish = try container.decode(String.self, forKey: .garnish)
        self.coverPhoto = try container.decode(String.self, forKey: .coverPhoto)
        self.id = UUID() // cruical change
    }
}

相关问题