Swift: 选择器新视图

Swift: Picker new View

对于每个 Struct,我都收到错误('GameOneView' 的重新声明无效)。同样在 ZStack 的括号中,我收到此错误(无法推断复杂闭包 return 类型;添加显式类型以消除歧义)此外,我的第三个错误是在标签处。(使用未解析的标识符 'Label' )

 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")}}

截图如下:

这不是一件容易的事:

    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")}}