Issue with passing variable from one view (accessed from a NavigationLink) to the parent view

  Kiến thức lập trình

I’m learning Swift and am trying to experiment. I’m trying to access a variable and pass it from a subview in a NavigationLink. The variable is selected/controlled from a list within the view in the NavigationLink, and I want it to be passed back to the main screen view (ContentView) for use in code there. I’ve tried using @Binding and @State, but I keep getting an error at the bottom of the ContentView file saying “Missing argument for parameter ‘selection’ in call”. Can someone help?

ContentView Code:

`import SwiftUI
import AVKit

struct ContentView: View {
    @State var tempo = Int(60)
    @State var onOff = false
    @State var searchText = ""
    @Binding var selection: String
  

    var body: some View {
        
        VStack {
            
            if onOff == false {
                
                Text("🟠🟠🟠🟠🟠🟠")
            }
            else {
                Text("🔵🔵🔵🔵🔵🔵")
            }
                
            NavigationView{
                NavigationLink(destination: talamList()){
                    Text(talamList().selection ?? "Talams")
                }
            }
            VStack{
                    Text("⏲")
                    Stepper {
                        Text("Tempo: (tempo) BPM")
                    }
                    onIncrement: {
                        if tempo < 200{
                            tempo = tempo + 1
                        }
                    }
                    onDecrement: {
                        if tempo > 1{
                            tempo = tempo - 1
                        }
                    }
                }
                    
                    
    
            
            Toggle(isOn: $onOff) {
                Text("Metronome Status: ")
            }
        }
        .padding()
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }

}`



talamList code:

`import SwiftUI

struct talamList: View {
    @State var searchText = ""
    @State var selection: String?

    let talamDict = ["tisra triputa" : 7, "chatusra triputa (adi)" : 8, "khanda triputa" : 9, "misra triputa" : 11, "sankeerna triputa" : 13, "adi (2 kalai)" : 16, "tisra ekam" : 3, "chatusra ekam" : 4, "khanda ekam" : 5, "misra ekam" : 7, "sankeerna ekam" : 9, "tisra jhampa" : 6, "chatusra jhampa" : 7, "khanda jhampa" : 8, "misra jhampa" : 10, "sankeerna jhampa" : 12]
    

        
    var searchResults: [String] {
        let talams = Array(talamDict.keys)
        if searchText.isEmpty {
            return talams
        }
        else {
            return talams.filter{$0.contains(searchText.lowercased())}
        }
    }
    
    
    var body: some View {
        NavigationStack{
            List (searchResults, id: .self, selection: $selection){ talam in
                Text(talam)
            }
            .listStyle(.plain)
            .searchable(text: $searchText)
            .navigationTitle("Talams")
        }
    }
}

#Preview {
    talamList()
}`

New contributor

Surya Ganesan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT