0

I have the following view ModalView opened by by a parent view ParentView using a button in the toolbar

** Parent View **

import SwiftUI

struct ParentView: View {
    
    @EnvironmentObject var environmentObject: MainStore
    @State private var showCreationView = false
    
    var body: some View {
        NavigationView {
            Text("ENV_OBJ Count: \(environmentObject.shoppingChartFullList.count)")
            .navigationBarTitle(Text("Navigation Bar Title"))
            .toolbar {
                ToolbarItem(placement: .bottomBar) {
                    Button(action: { showCreationView = true }) {
                        Image(systemName: "plus")
                    }
                    
                    .sheet(isPresented: $showCreationView, content: {
                        ModalView(showModelView: $showCreationView)
                    })
                }
            }
        }
    }
}

struct ParentView_Previews: PreviewProvider {
    static var previews: some View {
        ParentView().environmentObject(MainStore())
    }
}

** Modal View **

import SwiftUI

struct ModalView: View {
    
    @EnvironmentObject var environmentObject: MainStore
    @Binding var showModelView: Bool
    
    @State var newItem = ShoppingChartModel()
    
    var body: some View {
        Text(/*@START_MENU_TOKEN@*/"Hello, World!"/*@END_MENU_TOKEN@*/)
        Button(action: {
            
            environmentObject.shoppingChartFullList.append(newItem)
            self.showModelView = false

          }) {Text("Salva")}
    }
}

struct ModalView_Previews: PreviewProvider {
    static var previews: some View {
        ModalView(showModelView: .constant(true)).environmentObject(MainStore())
    }
}

** Main Store **

import Foundation
import SwiftUI
import Combine

final class MainStore: ObservableObject {
    //An observable object needs to publish any changes to its data, so that its subscribers can pick up the change.
    @Published var shoppingChartFullList: [ShoppingChartModel] = load()
}

The problem is that, action executed by the Save Button in the Modal View doesn't dismiss the modal (even if it correctly updates both the Boolean variable and the Env_Obj).

Seems to be related with the toolbar... in fact if I remove the Navigation View and the toolbar, putting the button directly in a stack ... it works...

In the Parent2View I remove the NavigationView and just configured the button directly in the view after the Text property. And it is working as expected.

import SwiftUI

struct ParentView2: View {
    
    @EnvironmentObject var environmentObject: MainStore
    @State private var showCreationView = false
    
    var body: some View {
        VStack {
            Text("ENV_OBJ Count: \(environmentObject.shoppingChartFullList.count)")
            Button(action: { showCreationView = true }) {
                Image(systemName: "plus")
            }
            .sheet(isPresented: $showCreationView, content: {
                ModalView(showModelView: $showCreationView)
            })
        }
    }
}

struct ParentView2_Previews: PreviewProvider {
    static var previews: some View {
        ParentView2().environmentObject(MainStore())
    }
}

UPDATE Ok I finally found the solution. The issue is the toolbar. If the ParentView is modified as follow, all start working well

struct ParentView: View {
    
    @EnvironmentObject var environmentObject: MainStore
    @State private var showCreationView = false
    
    var body: some View {
        NavigationView {
            Text("ENV_OBJ Count: \(environmentObject.shoppingChartFullList.count)")
            .navigationBarTitle("Nav View Title")
            .navigationBarItems(trailing:
                Button(action: {
                    self.showCreationView.toggle()
                })
                {
                    Image(systemName: "plus")
                        .font(Font.system(.title))
                }
            )
        }
        .sheet(isPresented: $showCreationView) {
                    ModalView(showModelView: $showCreationView)
                }
    }
}

Thanks! Cristian

Cristian
  • 1
  • 2
  • Why is `newShoppingChartModel` An `@state` and not just a local variable in the action closure? – Paulw11 Jul 30 '21 at 10:24
  • 1
    Can you reduce the problem to a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example)? The code you post looks like excerpts right from your app and it is difficult to reproduce since we don't have access to your MainStore etc. – Damiaan Dufaux Jul 30 '21 at 10:26
  • Yes, show the main store. It looks to me like the update to the store array is causing the view state to change so the sheet isn't hidden and vice-versa if you swap the order of the lines – Paulw11 Jul 30 '21 at 10:31
  • I've updated the first post with the suggestion of Damiaan and Paulw11. – Cristian Jul 30 '21 at 12:11

0 Answers0