Swift: Picker new View

Here, it was not Easy Task:

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

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

enter image description here

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top