我正在构建一个食谱应用程序,其构建过程如下:
首先,我在一个单独的结构中创建了一个基本视图,并以Image作为背景,并在顶部显示了一些文本。然后我创建了一些水平ScrollViews,在其中嵌入了这些基本视图的视图。
我想要的是,当用户点击其中一个视图时,他们会根据选择进入详细信息屏幕。为此,我将视图的所有代码以及图像和文本嵌入navigationLink中。但是当我在模拟器中运行该应用程序并按其中一个视图时,没有任何反应。

这是带有文本和图像的视图的代码:

NavigationLink(destination: DetailView(title: titleText)) {
            ZStack {
                Image(foodImageName)
                    .resizable()
                    .renderingMode(.original)

                VStack {
                    Text(titleText)
                        .foregroundColor(Color.init(hex: "AAAAAA"))
                        .font(.system(size: 30, weight: .semibold))
                    Spacer()
                }
            }
            .frame(width: 189, height: 194)
            .cornerRadius(15)
            .shadow(color: Color.init(hex:"000000"), radius: 7,x: 7, y: 7)
        }

这是主视图的代码:
struct ContentView: View {

    private var spacing: CGFloat = 20
    var body: some View {
        NavigationView {
            VStack {
                Text("First Course")
                    .frame(height: 36)
                    .font(.system(size: 25, weight: .semibold))

                ScrollView (Axis.Set.horizontal, showsIndicators: false){
                    HStack{
                        MenuTopicView(titleText: "Soup", foodImageName: "Soup")
                        MenuTopicView(titleText: "Fish", foodImageName: "Soup")
                    }
                }.frame(height: 200)

                Text("Main Course")
                    .frame(height: 36)
                    .font(.system(size: 25, weight: .semibold))

                ScrollView(Axis.Set.horizontal) {
                    HStack {
                        MenuTopicView(titleText: "Steak", foodImageName: "Soup")
                        MenuTopicView(titleText: "Chicken", foodImageName: "Soup")
                    }
                }

                Text("Dessert")
                    .frame(height: 36)
                    .font(.system(size: 25, weight: .semibold))

                ScrollView(Axis.Set.horizontal) {
                    HStack {
                        MenuTopicView(titleText: "Ice cream", foodImageName: "Soup")
                        MenuTopicView(titleText: "Pancakes", foodImageName: "Soup")
                    }
                }
            }
        .navigationBarTitle("Recipes")
        }
    }

}

有人对我做错了什么建议吗?
谢谢!

最佳答案

您应该将每个MenuTopicView包裹在其单独的NavigationLink中,然后将其添加到ContentView中,以便它们起作用,因为它们都有各自的目的地。像这样:

            ScrollView (Axis.Set.horizontal, showsIndicators: false){
                HStack{
                    NavigationLink(destination: DetailView("Soup")) {
                        MenuTopicView(titleText: "Soup", foodImageName: "Soup")
                    }
                    NavigationLink(destination: DetailView("Fish")) {
                        MenuTopicView(titleText: "Fish", foodImageName: "Soup")
                    }
                }
            }.frame(height: 200)

我建议创建更通用的视图,这样您就不必重复自己了。像这样:
struct ContentView: View {
    let sections: [MenuSection]

    private var spacing: CGFloat = 20
    var body: some View {
        NavigationView {
            VStack {
                ForEach(sections, id: \.self) { section in
                    MenuSectionView(sectionTitle: section.title, items: section.items)
                }

            }
            .navigationBarTitle("Recipes")
        }
    }
}

struct MenuSectionView: View {
    let sectionTitle: String
    let items: [MenuItem]

    var body: some View {
        VStack {
            Text(sectionTitle)
                .frame(height: 36)
                .font(.system(size: 25, weight: .semibold))
            ScrollView (Axis.Set.horizontal, showsIndicators: false) {
                HStack {
                    ForEach(items, id: \.self) { item in
                        NavigationLink(destination: RecipeView(recipeName: item.title)) {
                            MenuTopicView(titleText: item.title, foodImageName: item.image)
                        }
                    }
                }
            }.frame(height: 200)
        }
    }
}

关于ios - 如何使用包含某些布局的ZStack的NavigationLink导航到详细信息屏幕,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59783652/

10-09 15:49