将变量从列表行传递到下一个视图。 SwiftUI

问题描述 投票:0回答:1

嗨,我是 SwiftUI 新手,我使用 List 和 Grid/GridRow 创建了一个表。该表由简单的玩家模型填充。上面只有玩家的名字、姓氏和分数。当用户单击特定玩家行时,应用程序应打开模态视图并显示有关该特定玩家的信息。

我遇到的问题是,无论单击哪一行,模态视图都会显示第一个玩家的信息。此时,我只是将名字传递给模态视图。当我打印以控制台传递给模态的变量时,它根据单击的行显示正确的名称。但控制台视图只会在第一行显示名称。

第一行的名字是“Roy”,第二行是“Richard”。如果我单击第 2 行,控制台会打印出“Richard”,但模态视图显示“Roy”

这是我的代码 导入 SwiftUI

    struct PlayerView: View {

    let players = [
        Players(firstName: "Roy", lastName: "Kent",score:100),
        Players(firstName: "Richard", lastName: "Montlaur",score:100),
        Players(firstName: "Dani", lastName: "Rojas",score:100)
    ]
    @State private var showingReportCard = false

    var body: some View {
        NavigationStack {
            List{
                Grid{
                    GridRow {
                        Text("First Name")
                        Text("Last Name")
                        Text("Score")
                    }
                    Divider()
                    ForEach(players){
                        player in
                        GridRow {
                            Text(player.firstName)
                            Text(player.lastName)
                            Text("\(player.score)")
                        }.onTapGesture {
                       
                        showingReportCard.toggle()
                        print(player.firstName)
                        }.sheet(isPresented: $showingReportCard) {
                        TutoringView(firstName:player.firstName)
                        }
                    }
                
                }
            }

        }
    }
}
swift swiftui gridview modalviewcontroller
1个回答
0
投票

如评论中所述,使用 .sheet(item: ...) 并将此工作表调用移至 NavigationStack。

struct ContentView: View {
    var body: some View {
        PlayerView()
    }
}

struct Players: Identifiable {
    let id = UUID()
    var firstName: String
    var lastName: String
    var score: Int
}

struct PlayerView: View {
    let players = [
        Players(firstName: "Roy", lastName: "Kent",score:100),
        Players(firstName: "Richard", lastName: "Montlaur",score:100),
        Players(firstName: "Dani", lastName: "Rojas",score:100)
    ]

    @State private var selectedPlayer: Players?  // <---
    
    var body: some View {
        NavigationStack {
            List{
                Grid{
                    GridRow {
                        Text("First Name")
                        Text("Last Name")
                        Text("Score")
                    }
                    Divider()
                    ForEach(players){ player in
                        GridRow {
                            Text(player.firstName)
                            Text(player.lastName)
                            Text("\(player.score)")
                        }.onTapGesture {
                            print(player.firstName)
                            selectedPlayer = player  // <---
                        }
                    }
                }
            }
        }
        .sheet(item: $selectedPlayer) { player in  // <---
            TutoringView(firstName: player.firstName)
        }
    }
}

struct TutoringView: View {
    @Environment(\.dismiss) var dismiss
    var firstName: String
    
    var body: some View {
        Text(firstName)
        Button("dismiss") {
            dismiss()
        }.buttonStyle(.bordered)
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.