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'的值) 我添加了2.0.0版的代码!试试看它是否有效! 感谢您的努力,但如果我只是复制粘贴,则会出现多个错误。 我看到了你的截图,是因为Label,我认为你没有使用苹果设备,或者类似的东西,我怎么把那个Label去掉,换成Text,现在它应该为你工作,事实上你身边有错误,即使没有问题,你也可以运行 V1,但你似乎没有使用正确的设备或软件。 Aber Gerne,Viel Spaß mit SwiftUI

以上是关于Swift:选择器新视图的主要内容,如果未能解决你的问题,请参考以下文章

在 Swift 的选择器视图中显示 API 的名称

选择器视图默认行未使用 swift 2 更改

在 Swift 上更改选择器视图的颜色

如何在 swift 3.0 中制作时间选择器视图?

Swift 中的文档选择器视图控制器问题

日期选择器视图超小 - Swift Xcode