我想在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的新手,还是初学者。预先感谢。
可能类似于以下内容
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")
}