Swift UI应用程序导航链接被禁用

huangapple go评论69阅读模式
英文:

Swift UI Application Navigation Link is disable

问题

我试图在用户点击单元格时切换到详细视图,但我不知道为什么它被禁用,当我点击特定单元格时无法切换到详细视图...

这是我的内容视图的代码:

struct ContentView: View {
    @EnvironmentObject private var viewModel: FruitListViewModel

    var body: some View {
        VStack {
            Text("Fruit List")
                .font(.largeTitle)
            List {
                ForEach(viewModel.fruits) { fruit in
                    NavigationLink(destination: FruitDetailsView(fruit: fruit)) {
                        RowView(name: fruit.name, genus: fruit.genus, family: fruit.family)
                    }
                }
            }
        }
        .onAppear {
            viewModel.fetchFruit()
        }
    }
}

这是RowView的代码:

import SwiftUI

struct RowView: View {
    let name: String
    let genus: String
    let family: String

    var body: some View {
        VStack(alignment: .leading) {
            HeadTitleView(name: name)
            Text("Genus: \(genus)")
            Text("Family: \(family)")
            Spacer()
        }
    }
}

这是Details view的代码:

import SwiftUI

struct FruitDetailsView: View {
    @Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
    @State private var name: String = ""
    @State private var genus: String = ""
    @State private var family: String = ""

    let fruit: Fruits

    var body: some View {
        VStack(alignment: .center, spacing: 5) {
            Text("Name: \(fruit.name)")
                .foregroundColor(Color.gray)
            Text("Genus: \(fruit.genus)")
                .foregroundColor(Color.gray)
            Text("Family: \(fruit.family)")
                .foregroundColor(Color.gray)
                .padding(50)
        }
        .onAppear(perform: {
            name = fruit.name
            genus = fruit.genus
            family = fruit.family
        })
        .padding(20)
    }
}

这是屏幕截图...

Swift UI应用程序导航链接被禁用

英文:

I am trying to move to details view when user click the cell but I do not know why it disable and not able to move into details view when I click the particular cell form UI view ..

Here is my code for content view ..

struct ContentView: View {
    @EnvironmentObject private var viewModel: FruitListViewModel

    var body: some View {
        VStack {
            Text(&quot;Fruit List &quot;)
                .font(.largeTitle)
            List {
                ForEach(viewModel.fruits) { fruit in
                    
                    NavigationLink(destination: FruitDetailsView(fruit: fruit)) {
                        RowView(name: fruit.name, genus: fruit.genus, family: fruit.family)
                    }
                }
            }
        }
        .onAppear {

            viewModel.fetchFruit()
        }

    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Here is the code for RowView ..

import SwiftUI

struct RowView: View {

    let name: String
    let genus: String
    let family: String

    var body: some View {

        VStack(alignment: .leading) {
            HeadTitleView(name: name)
            Text(&quot;Genus: \(genus)&quot;)
            Text(&quot;family:\(family)&quot;)
            Spacer()
        }
    }
}
struct RowView_Previews: PreviewProvider {
    static var previews: some View {
        RowView(name: &quot;Apple&quot;, genus: &quot;Apple&quot;, family: &quot;Appicote&quot;)
    }
}

Here is the code for Details view ..

import SwiftUI

struct FruitDetailsView: View {
    @Environment(\.presentationMode) var presentationMode: Binding&lt;PresentationMode&gt;
    @State private var name: String = &quot;&quot;
    @State private var genus: String = &quot;&quot;
    @State private var family: String = &quot;&quot;

    let fruit: Fruits

    var body: some View {

        VStack(alignment: .center, spacing: 5) {
                Text(&quot;Name: \(fruit.name)&quot;)
                    .foregroundColor(Color.gray)
                Text(&quot;Genus: \(fruit.genus)&quot;)
                    .foregroundColor(Color.gray)

                Text(&quot;Family: \(fruit.family)&quot;)
                .foregroundColor(Color.gray).padding(50)

        }.onAppear(perform: {
            name = fruit.name
            genus = fruit.genus
            family = fruit.family
        }).padding(20)
    }
}

Here is the screenshot ..
Swift UI应用程序导航链接被禁用

答案1

得分: 0

NavigationLink 只有在它们位于 NavigationView 中才能正常工作。只需将您的 VStack 包装在 NavigationView 中,这样应该能正常工作。

struct ContentView: View {
    var body: some View {
        NavigationView { // &lt;---
            VStack {
                List {
                    ForEach(...) { fruit in
                        NavigationLink(...) { // &lt;-- 链接位于 NavigationView 内
                            RowView()
                        }
                    }
                }
            }
        }
        .onAppear {
           ...
        }
    }
}
英文:

NavigationLinks only work when they are in a NavigationView. Simply wrap your VStack in a NavigationView and this should work nicely.

struct ContentView: View {
    var body: some View {
        NavigationView { // &lt;---
            VStack {
                List {
                    ForEach(...) { fruit in
                        NavigationLink(...) { // &lt;-- Link is inside the NavigationView
                           RowView()
                        }
                    }
                }
            }
        }
        .onAppear {
           ...
        }
    }
}

huangapple
  • 本文由 发表于 2023年3月7日 20:21:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75661915.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定