What is the best and simple way to link a SwiftUI button action to a View Model?

im having so much trouble trying to figure pout how I can get a SwiftUI button to link to a different SwiftUI file / view that is giving me so much trouble.


'


This is the Code I was attempting to bridge them with but I keep ending up with many different errors. So what am I not getting... there has to be a way to do this if not then what's the point of using SwiftUI?



Replies

trying to implement it like this:

Code Block import UIKit
import SwiftUI
import Combine
struct SwiftUIView: View {
    @State var showSheetView = false
    var body: some View {
        NavigationView {
            Text("Content")
            .navigationBarTitle("SwiftUI Tutorials")
            .navigationBarItems(trailing:
                Button(action: {
                    self.showSheetView.toggle()
                }) {
                    Image(systemName: "bell.circle.fill")
                        .font(Font.system(.title))
                }
            )
        }.sheet(isPresented: $showSheetView) {
            SheetView(showSheetView: self.$showSheetView)
        }
    }
}
struct SheetView: View {
    @Binding var showSheetView: Bool
    @ObservedObject private(set) var viewModel: ViewModel
    @State private var isRefreshing = false
    
    var body: some View {
        NavigationView {
            List(viewModel.videos.sorted { $0.id > $1.id}, id: \.id) { video in
                NavigationLink(
                destination: VideoDetails(viewModel: VideoDetails.ViewModel(video: video))) {
                    VideoRow(video: video)
                }
            }
            .onPullToRefresh(isRefreshing: $isRefreshing, perform: {
                self.viewModel.fetchVideos()
            })
            .onReceive(viewModel.$videos, perform: { _ in
                self.isRefreshing = false
            })
            .navigationBarTitle(viewModel.navigationBarTitle)
        }
        .onAppear(perform: viewModel.fetchVideos)
        .sheet(isPresented: $showSheetView) {
                    SheetView(showSheetView: self.$showSheetView)
                }
    }
}


leads to: Cannot find type 'ViewModel' in scope & The compiler is unable to type-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions; on line 35 and 38.

This

Code Block import UIKit
import SwiftUI
import Combine
struct ContentView: View {
    @State private var isPresented = false
var body: some View {
    Button("Show Modal with full screen") {
        self.isPresented.toggle()
    }
    .fullScreenCover(isPresented: $isPresented, content: VideoList.init)
    }
}
struct VideoList: View {
     @Environment(\.presentationMode) var presentationMode
     @ObservedObject private(set) var viewModel: ViewModel
     @State private var isRefreshing = false
var body: some View {
    NavigationView {
        List(viewModel.videos.sorted { $0.id > $1.id}, id: \.id) { video in
            NavigationLink(
            destination: VideoDetails(viewModel: VideoDetails.ViewModel(video: video))) {
                VideoRow(video: video)
            }
        }
        .onPullToRefresh(isRefreshing: $isRefreshing, perform: {
            self.viewModel.fetchVideos()
        })
        .onReceive(viewModel.$videos, perform: { _ in
            self.isRefreshing = false
        })
        .navigationBarTitle(viewModel.navigationBarTitle)
    }
    .onAppear(perform: viewModel.fetchVideos)
    .frame(maxWidth: .infinity, maxHeight: .infinity)
    .background(Color.red)
    .edgesIgnoringSafeArea(.all)
    .onTapGesture {
        presentationMode.wrappedValue.dismiss()
    }
}
}
#if DEBUG
struct VideoList_Previews: PreviewProvider {
    static var previews: some View {
        NavigationView {
            VideoList(viewModel: .init())
        }
    }
}
#endif


creates this error on line 19: Cannot convert value of type '(Environment<Binding<PresentationMode>>, VideoList.ViewModel) -> VideoList' to expected argument type '() -> VideoList'

Does anyone have a clue? I've spent the last 3 days trying to solve this code none of the WWDC video even come closed to explaining what's going on