UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Using data collected from an API in subsequent views

Forums > Swift

Forgive any noobness here. I'm new to Swift blah blah. I'm trying to go through the 100 days course and it's great BTW.

So I have a function that calls and API and stores the results in a STRUT. It's working fine. :)

Thing is, i'd like to present the data in that STRUT on another view (or maybe multiple other views) in the app.

If anyone can point me in the right direction as to what I need to do i'd be grateful.

In fact my plan for the app is that the function that calls the API collects all of the data any subsequent views will need (i'll need to call a few different APIs to gather all the info that i'll ultimately need)

Any insights most welcome :)

Thank you!

2      

here is your detail view you need the data for , you use @Binding here

import SwiftUI

struct DetailView: View {
    @Binding var movie: MovieData

    var body: some View {
        ZStack {
            VStack {

                Text(movie.title)
                    .font(.title)
                    .padding()
                Text(movie.overview)
                    .padding()

            }

        }
    }

}

here is your list view , all you need here is now to make sure you pass a Movie Object on navigation

import SwiftUI

struct Movies: View {

    var body: some View {
        NavigationStack {
            ScrollView {

            }

            .navigationDestination(isPresented: $gotoDetails) {
                DetailView(movie: $mov)   <------ make a state property and when you are looping over the array like  ForEach(viewModel.results, id:\.id)  equate it with the state variable 
            }

}

2      

Thank you but my use case is not exactly the master-detail view.

Here's what I have:

I have a view that prompts for login credentials. Once provided the view calls a function, passing the creds, and that function connects to an API and decodes the response. Once the API is read I then programatically select a second tab on the tab bar to open up that tab.

I would like to display the data acquired from the API in that newly opened view.

Thank you

2      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.