SwiftUI:传递字符串以在按下按钮时查看

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

我想在SwiftUI中的按钮上将字符串从ContentView传递到SoundPageView。每个不同的按钮都应传递不同的字符串。这是一个英语学习应用程序,所以我想通过一个语法类别。

例如。

                NavigationLink(destination: SoundPageView()) {
                    Text("Should")
                }
                .buttonStyle(GrammarButton())
                NavigationLink(destination: SoundPageView()) {
                    Text("Articles)
                }
                .buttonStyle(GrammarButton())
                NavigationLink(destination: SoundPageView()) {
                    Text("First Conditional")
                }
                .buttonStyle(GrammarButton())

[我的SoundPageView是一个具有很多播放声音的按钮的视图。

struct SoundPageView: View {

    var body: some View {
        NavigationView {
                HStack(spacing:0) {
                    VStack {
                        Button(action: {
                            playSound(sound: "art_audio1", type: "mp3")
                        }) {
                            Text("Button 1")
                        }

我想将诸如“应该”,“艺术”或“第一”的类别字符串传递给SoundPageView,以便为按钮加载不同的声音。

我也想使用类别字符串来更改音频文件名,但是我不确定该怎么做。有点像...

playSound(声音:\ category +“ _audio1”,键入:“ mp3”)

我是SwiftUI的新手,还是初学者。预先感谢。

string view swiftui
1个回答
0
投票

可能类似于以下内容

NavigationLink(destination: SoundPageView(category: "should")) {
    Text("Should")
}
.buttonStyle(GrammarButton())
NavigationLink(destination: SoundPageView(category: "art")) {
    Text("Articles)
}
.buttonStyle(GrammarButton())
NavigationLink(destination: SoundPageView(category: "first")) {
    Text("First Conditional")
}
.buttonStyle(GrammarButton())

struct SoundPageView: View {
    var category: String

    var body: some View {
        NavigationView {
                HStack(spacing:0) {
                    VStack {
                        Button(action: {
                            playSound(sound: "\(self.category)_audio1", type: "mp3")
                        }) {
                            Text("Button 1")
                        }
© www.soinside.com 2019 - 2024. All rights reserved.