如何在SwiftUI中正确使用NavigationLink
全新的编码器和第一次使用。如果这对大多数人来说太容易了,我道歉。我在使用naviationLink从上下文视图切换到详细视图时遇到问题。我想显示一排吉他,然后显示从表中选择的吉他的详细视图 这里有一些细节。我有一个Guitars.swift文件,其中包含吉他列表。(见下文) 我有一个包含已定义属性和结构的GuitardDetails文件。(见下文)如何在SwiftUI中正确使用NavigationLink,swift,navigation,swiftui,Swift,Navigation,Swiftui,全新的编码器和第一次使用。如果这对大多数人来说太容易了,我道歉。我在使用naviationLink从上下文视图切换到详细视图时遇到问题。我想显示一排吉他,然后显示从表中选择的吉他的详细视图 这里有一些细节。我有一个Guitars.swift文件,其中包含吉他列表。(见下文) 我有一个包含已定义属性和结构的GuitardDetails文件。(见下文) 我得到的错误是:“'GuitarDetails.Type'不能转换为”(Guitars)->GuitarDetails'你需要传递你的吉他数组,而不
我得到的错误是:“'GuitarDetails.Type'不能转换为”(Guitars)->GuitarDetails'你需要传递你的
吉他数组,而不是列表中的吉他类型。这就是为什么你会得到这个错误
虽然您的命名不好,但您使用复数表示单数项,单数表示复数项。这可能会导致混淆,最好使用适当的术语来描述您所描述的内容
例如,您的Guitars
结构只描述了一把吉他,因此它实际上应该被重命名为Guitars
。类似地,吉他数组被命名为Guitars
,这没有意义,因为它是一个吉他数组,所以它应该是复数,并被命名为Guitars
理想情况下,您应该有这样的东西;注意,我使用了更合适的命名约定
struct Guitar: Identifiable, Hashable {
let id: Int
let name, description, imageName: String
}
// defining guitars like this, outside of a class or struct, makes it a global variable.
let guitars: [Guitar] = [
.init (id: 1, name: "Classical", description: "This is a nylon string guitar that is acoustic and made if cedar or spruce wood", imageName: "classical"),
.init (id: 2, name: "Acoustic", description: "The acoustic guitar is a steel string instrument that is traditionally played as an accompanying part of a musical performance.", imageName: "acoustic"),
.init (id: 3, name: "Electric", description: "This is a steel string guitar that is electic with pickups and made to be played through amplifiers", imageName: "electric_guitar-1"),
.init (id: 4, name: "Banjo", description: "This is a steel string guitar that has five strings. It's usually played in country and bluegrass music", imageName: "banjo"),
.init (id: 5, name: "Bass", description: "This is a steel string guitar that has four strings. It's usually played in all types of bands and provides the bass line for the music", imageName: "bass")
]
然后,您的吉他尾部应该与此类似:
struct GuitarDetail: View {
var selectedGuitar: Guitar // notice it is no longer plural, as that didn't make sense
var body: some View {
VStack{
Image(selectedGuitar.imageName) // make sure you use the image name
.resizable()
.frame(width:300, height: 300)
VStack {
Spacer()
}
}
}
}
最后是您的ContentView
struct ContentView: View {
var body: some View {
NavigationView {
// as you have defined guitars above as a global variable you should be able to access it like this
List (guitars) { guitar in
NavigationLink(destination: GuitarDetail(selectedGuitar: guitar) {
Text(guitar.name) // you need something inside your NavigationLink for it to work
}
}
}
}
谢谢你的帮助。我非常感谢。现在我的吉他细节文件中的预览结构在狂吠。有一个占位符,但我不确定它期望什么。它以前没有这样做。你能告诉我它期望什么,这样我就可以运行代码吗?struct Guitardeails\u预览:预览提供程序{静态变量预览:一些视图{GuitarDetails(selectedGuitar:Guitar)。错误是源文件中的编辑器占位符。您需要传入Guitar
对象。只需传入guitars[0]
就可以了,我试过了,它仍然显示相同的错误。GuitarDetails(selectedGuitar:Guitar[0])。我还用吉他[0]试过它它给了我一个关于未解析标识符guitars的错误。这很难。这取决于您所做的更改,您需要确保所有内容都正确命名。在我的代码版本中,guitars[0]
起作用,或者您可以创建一个Guitar
的实例并将其传入Guitar(id:1,名称:“古典”,描述:“这是一把尼龙弦吉他,是原声吉他,由雪松或云杉木制成”,imageName:“古典”)
好的。我会设法弄清楚的。再次感谢您的帮助。
struct Guitar: Identifiable, Hashable {
let id: Int
let name, description, imageName: String
}
// defining guitars like this, outside of a class or struct, makes it a global variable.
let guitars: [Guitar] = [
.init (id: 1, name: "Classical", description: "This is a nylon string guitar that is acoustic and made if cedar or spruce wood", imageName: "classical"),
.init (id: 2, name: "Acoustic", description: "The acoustic guitar is a steel string instrument that is traditionally played as an accompanying part of a musical performance.", imageName: "acoustic"),
.init (id: 3, name: "Electric", description: "This is a steel string guitar that is electic with pickups and made to be played through amplifiers", imageName: "electric_guitar-1"),
.init (id: 4, name: "Banjo", description: "This is a steel string guitar that has five strings. It's usually played in country and bluegrass music", imageName: "banjo"),
.init (id: 5, name: "Bass", description: "This is a steel string guitar that has four strings. It's usually played in all types of bands and provides the bass line for the music", imageName: "bass")
]
struct GuitarDetail: View {
var selectedGuitar: Guitar // notice it is no longer plural, as that didn't make sense
var body: some View {
VStack{
Image(selectedGuitar.imageName) // make sure you use the image name
.resizable()
.frame(width:300, height: 300)
VStack {
Spacer()
}
}
}
}
struct ContentView: View {
var body: some View {
NavigationView {
// as you have defined guitars above as a global variable you should be able to access it like this
List (guitars) { guitar in
NavigationLink(destination: GuitarDetail(selectedGuitar: guitar) {
Text(guitar.name) // you need something inside your NavigationLink for it to work
}
}
}
}