我正在为一个学校项目制作一个应用程序,我试图计算一个人在不同星球上的重量。ContentView 文件在预览中工作正常,但仅在模拟器/iPhone 中显示 NavigationBar.title。这是代码
import SwiftUI
struct ContentView : View {
var planets: [Planet] = []
var body: some View {
NavigationView {
List(planets) { planet in
PlanetCell(planet: planet)
}
.navigationBarTitle(Text("Sun"))
}
}
}
#if DEBUG
struct ContentView_Previews : PreviewProvider {
static var previews: some View {
ContentView(planets: testData)
}
}
#endif
struct PlanetCell : View {
let planet : Planet
var body: some View {
return NavigationLink(destination: PlanetDetail(planet: planet)){
Text(planet.name)
}
}
}
编辑名为 Planet 的 swift 文件
import Foundation
import SwiftUI
struct Planet : Identifiable {
var id = UUID()
var name: String
var g : Float
var imageName : String { return name }
}
#if DEBUG
let testData = [
Planet(name: "Mercury", g: 3.724),
Planet(name: "Venus", g: 8.918),
Planet(name: "Earth", g: 9.8),
Planet(name: "Mars", g: 3.724),
Planet(name: "Jupiter", g: 22.932),
Planet(name: "Saturn", g: 9.114),
Planet(name: "Uranus", g: 9.016),
Planet(name: "Neptune", g: 10.976)
]
#endif