BLACK FRIDAY: Save 50% on all my Swift books and bundles! >>

SOLVED: Passing Properties Between Views

Forums > SwiftUI

I'm going to try and break this down in as simple terms as possible.

Let's say I have a Struct with this property in it

@State private var selectedGifName: String?

When a user taps an item on the screen, this property updates with a simple string containing the file name of a gif image.

Now, I have a Class in a different file of my app that makes a POST request to an API. One of the parameters of that POST request takes the gif image name contained in that State variable from the other file's Struct.

What is the most appropriate way to pass the value of selectedGifName from my Struct, into the other file's Class where the POST request is being done.

I've tried:

-Calling the Struct directly as a new variable inside the Class, but I'm required to include parameters for every property of that Struct which isn't necessary.

-Messed with @StateObject assignements but this doesn't work when a Struct has something a Class wants.

The connections between View files and ViewControllers is making my brain hurt. That's mainly what this comes down to. When structuring a good MVVM app, I'm really having problems understanding how different Structs and Classes can pass details about properties to one another.

3      

Probably as a parameter to the method on your class that makes the API call.

You should have a ViewModel ObservableObject class that handles the API stuff and pass it a parameter from the View struct when the @State variable changes.

The general outline of that would be something like:

class ViewModel: ObservableObject {
    @Published var apiResult: Whatever

    func fetchAPIData(for param: ParamType) {
        //do API stuff and assign result to apiResult
    }
}

struct ExampleView: View {
    @StateObject var viewModel: ViewModel()
    @State private var selectedGifName: String?

    var body: some View {
        TextField("GIF Name", text: $selectedGifName)
            .onChange(selectedGifName) { _ in
                if let selectedGifName = selectedGifName {
                    viewModel.fetchAPIData(selectedGifName)
                }
            }
        //other View stuff that uses viewModel.apiResult
    }
}

The connections between View files and ViewControllers is making my brain hurt.

You're using SwiftUI, you don't need to think about ViewControllers at all.

3      

Thank you RB.

Would you mind giving me some more insight about your last statement that SwiftUI means I don't need to worry about ViewControllers?

3      

UIKit (and other pre-SwiftUI Apple frameworks) uses ViewControllers as part of the MVC pattern. ViewControllers sit between the model and the view and coordinates things between them.

SwitUI does away with ViewControllers. Instead it uses declarative View structs, which are essentially blueprints for how your interface should be rendered onscreen. There is no ViewController to mediate between a View and a model in SwiftUI; instead, it uses data binding and reactive programming concepts to handle the kinds of things ViewControllers took care of before.

You only need to worry about ViewControllers in SwiftUI when bridging between it and older stuff that doesn't have an equivalent (yet). Then you would use a (UI|NS)ViewControllerRepresentable to wrap the old ViewController and allow it to work in the newer SwiftUI environment.

3      

Save 50% in my WWDC sale.

SAVE 50% All our books and bundles are half price for Black Friday, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

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.