Swift:选择器新视图
Posted
技术标签:
【中文标题】Swift:选择器新视图【英文标题】:Swift: Picker new View 【发布时间】:2020-11-06 18:17:34 【问题描述】:对于每个结构,我都会收到错误消息('GameOneView' 的重新声明无效)。同样在 ZStack 的括号中,我收到此错误(无法推断复杂的闭包返回类型;添加显式类型以消除歧义)此外,我的第三个错误是在标签处。(使用未解析的标识符“标签”)
import SwiftUI
struct ContentView: View
@State var games = ["Game One", "Game Two", "Game Three", "Game Four", "Game Five"]
@State var selectedGame = 0
var body: some View
NavigationView
ZStack
Form
Section(header: Label("You selected:", systemImage: "gamecontroller"))
NavigationLink(destination: gameFinderV3(game: $games[selectedGame]) ) Text(games[selectedGame])
Picker(selection: $selectedGame, label: Text(""))
ForEach(0 ..< games.count) index in Text(games[index])
.navigationTitle(Text("All Games"))
struct gameFinderV3: View
@Binding var game: String
var body: some View
return Group
if game == "Game One"
GameOneView()
else if game == "Game Two"
GameTwoView()
else if game == "Game Three"
GameThreeView()
else if game == "Game Four"
GameFourView()
else if game == "Game Five"
GameFiveView()
else
EmptyView()
struct GameOneView: View var body: some View Text("GameOneView")
struct GameTwoView: View var body: some View Text("GameTwoView")
struct GameThreeView: View var body: some View Text("GameThreeView")
struct GameFourView: View var body: some View Text("GameFourView")
struct GameFiveView: View var body: some View Text("GameFiveView")
这是一个截图:
【问题讨论】:
【参考方案1】:在这里,这不是简单的任务:
import SwiftUI
struct ContentView: View
var games = ["Game One", "Game Two", "Game Three", "Game Four", "Game Five"]
@State var selectedGame = 0
var body: some View
NavigationView
ZStack
Form
Section(header: Label("You selected:", systemImage: "gamecontroller"))
NavigationLink(destination: gameFinder(game: games[selectedGame]) ) Text(games[selectedGame])
Picker(selection: $selectedGame, label: Text(""))
ForEach(0 ..< games.count) index in Text(games[index])
.navigationTitle(Text("All Games"))
func gameFinder(game: String) -> AnyView
if game == "Game One"
return AnyView(GameOneView())
else if game == "Game Two"
return AnyView(GameTwoView())
else if game == "Game Three"
return AnyView(GameThreeView())
else if game == "Game Four"
return AnyView(GameFourView())
else if game == "Game Five"
return AnyView(GameFiveView())
else
return AnyView(EmptyView())
struct GameOneView: View var body: some View Text("GameOneView")
struct GameTwoView: View var body: some View Text("GameTwoView")
struct GameThreeView: View var body: some View Text("GameThreeView")
struct GameFourView: View var body: some View Text("GameFourView")
struct GameFiveView: View var body: some View Text("GameFiveView")
版本:2.1.0
import SwiftUI
struct ContentView: View
@State var games = ["Game One", "Game Two", "Game Three", "Game Four", "Game Five"]
@State var selectedGame = 0
var body: some View
NavigationView
ZStack
Form
Section(header: Text("You selected:"))
NavigationLink(destination: gameFinderV3(game: $games[selectedGame]) ) Text(games[selectedGame])
Picker(selection: $selectedGame, label: Text(""))
ForEach(0 ..< games.count) index in Text(games[index])
.navigationTitle(Text("All Games"))
struct gameFinderV3: View
@Binding var game: String
var body: some View
return Group
if game == "Game One"
GameOneView()
else if game == "Game Two"
GameTwoView()
else if game == "Game Three"
GameThreeView()
else if game == "Game Four"
GameFourView()
else if game == "Game Five"
GameFiveView()
else
EmptyView()
struct GameOneView: View var body: some View Text("GameOneView")
struct GameTwoView: View var body: some View Text("GameTwoView")
struct GameThreeView: View var body: some View Text("GameThreeView")
struct GameFourView: View var body: some View Text("GameFourView")
struct GameFiveView: View var body: some View Text("GameFiveView")
【讨论】:
维伦丹克奥米德。非常感谢您的帮助。不幸的是,我在导航链接上遇到错误。 (不能调用非函数类型'NavigationLink以上是关于Swift:选择器新视图的主要内容,如果未能解决你的问题,请参考以下文章