Search code examples
iosswiftswiftuiswiftui-navigationlink

SwiftUI NavigationLink not sending data to another view


This is my contentView.swift file. I am trying to send data from one view to another view using navigationLink but I can't. I am learning the swiftUI and iOS development. So I am confused what I did wrong.

import SwiftUI
import URLImage

struct Response: Codable {
var results: [Result]

}

struct Result: Codable {
var trackId: Int
var trackName: String
var collectionName: String
var artworkUrl100: URL
}

struct ContentView: View {
@State var results = [Result]()


var body: some View {
    NavigationView {
        List(results, id: \.trackId) { item in
            NavigationLink(
                destination: SingleView(item: item),
                label: {
                    HStack() {
                        URLImage(item.artworkUrl100) { image in
                            image
                                .resizable()
                                .aspectRatio(contentMode: .fill)
                                .frame(width: /*@START_MENU_TOKEN@*/100/*@END_MENU_TOKEN@*/, height: /*@START_MENU_TOKEN@*/100/*@END_MENU_TOKEN@*/, alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/)
                                .cornerRadius(5.0)
                        }
                        
                        VStack(alignment: .leading) {
                            Text(item.trackName)
                                .font(.headline)
                                
                            
                            Text(item.collectionName)
                                .font(.subheadline)
                                .foregroundColor(.secondary)
                                
                            
                            Spacer()
                        }
                        
                        
                    }
                })
            
        }
        .onAppear(perform: loadData)
        .navigationTitle("Native App")
    }
    
    
} 



func loadData() {
    guard let url = URL(string: "https://itunes.apple.com/search?term=arijit&entity=song") else {
        print("Invalid URL")
        return
    }
    let request = URLRequest(url: url)
    URLSession.shared.dataTask(with: request) { data, response, error in
        if let data = data {
            if let decodedResponse = try? JSONDecoder().decode(Response.self, from: data) {
                // we have good data – go back to the main thread
                DispatchQueue.main.async {
                    // update our UI
                    self.results = decodedResponse.results
                }

                // everything is good, so we can exit
                return
            }
        }

        // if we're still here it means there was a problem
        print("Fetch failed: \(error?.localizedDescription ?? "Unknown error")")
    }.resume()
}
}


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

I want to send value to SingleView.swift page . but no luck.

My SingleView.swift code is:

import SwiftUI

struct SingleView: View {
let item: Result

var body: some View {
    Text("")
}
}

struct SingleView_Previews: PreviewProvider {
static var previews: some View {
    SingleView(item: Result)
}
}

Please let me know where I did wrong. Thank you.


Solution

  • Working fine with some changes to SingleView

    import SwiftUI
    
    struct SingleView: View {
        let item: Result
    
        var body: some View {
            Text(item.trackName)
        }
    }
    
    struct SingleView_Previews: PreviewProvider {
        static var previews: some View {
            SingleView(item: Result(trackId: 1, trackName: "Track Name", collectionName: "Coll name", artworkUrl100: URL(string: "https://stackoverflow.com/questions/68556148/swiftui-navigationlink-not-sending-data-to-another-view")!))
        }
    }