英文:
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)
}
}
这是屏幕截图...
英文:
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("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()
}
}
}
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("Genus: \(genus)")
Text("family:\(family)")
Spacer()
}
}
}
struct RowView_Previews: PreviewProvider {
static var previews: some View {
RowView(name: "Apple", genus: "Apple", family: "Appicote")
}
}
Here is the code for 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)
}
}
答案1
得分: 0
NavigationLink
只有在它们位于 NavigationView
中才能正常工作。只需将您的 VStack
包装在 NavigationView
中,这样应该能正常工作。
struct ContentView: View {
var body: some View {
NavigationView { // <---
VStack {
List {
ForEach(...) { fruit in
NavigationLink(...) { // <-- 链接位于 NavigationView 内
RowView()
}
}
}
}
}
.onAppear {
...
}
}
}
英文:
NavigationLink
s 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 { // <---
VStack {
List {
ForEach(...) { fruit in
NavigationLink(...) { // <-- Link is inside the NavigationView
RowView()
}
}
}
}
}
.onAppear {
...
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论