Code Monkey home page Code Monkey logo

Comments (2)

nosenergies avatar nosenergies commented on August 17, 2024

ViewModel comes with the MVVM pattern that you should use when using SwiftUI. A viewmodel contains all the data that need to be displayed in the view.
There are a lot of articles about that:

from swiftui-navigation-stack.

matteopuc avatar matteopuc commented on August 17, 2024

you have mentioned programmatic push/pop in the examples, but it's not clear how you implemented the ObservedObject var viewModel: ViewModel, and what it's purpose is, and what function performBackgroundActivities() is doing, would it be same if you just called DispatchQueue.main.async { self.navigationStack.push(ChildView()) } inside button's action?, can you explain this more? or do we have to implement ViewModel logic ourselves

struct MyHome: View {
    @ObservedObject var viewModel: ViewModel
    @EnvironmentObject private var navigationStack: NavigationStack

    var body: some View {
        Button(action: {
            self.viewModel.performBackgroundActivities(withCallback: {
                DispatchQueue.main.async {
                    self.navigationStack.push(ChildView())
                }
            })
        }, label: {
            Text("START BG ACTIVITY")
        })
    }
}

Hi @par400 in the README you can find a silly example about programmatic push. What I called viewModel in that example is just an object (relying on the MVVM pattern, as stated by @nosenergies) that contains some logic (for example the performBackgroundActivities , which is just an example). What is important is that you can programmatically push and pop by directly accessing the navigation stack environment object. In other words, you can rewrite the example this way:

struct MyHome: View {
    @EnvironmentObject private var navigationStack: NavigationStack

    var body: some View {
        Button(action: {
            self.navigationStack.push(ChildView())
        }, label: {
            Text("START BG ACTIVITY")
        })
    }
}

And it will work as expected.

from swiftui-navigation-stack.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.