8

I want pass data on dismissing of presentViewController to previous screen. Here I would like to use block to pass data to previous screen as UIKitApp. But I'm not getting idea to pass data. What are the options we have to pass data to back?

struct ContentView: View {
    @State var showModel = false
    var body: some View {
        VStack {
            Button(action: {
                showModel.toggle()
            }, label: {
                Text("Show filters")
            }).sheet(isPresented: $showModel, content: {
                FilterView()
            })
        }
    }
}

struct FilterView: View {
    @Environment(\.presentationMode) var presentationMode

    var onDismiss: ((_ model: Filter) -> Void)?

    var body: some View {
        
        VStack {
            Button(action: {
                // Pass data from here to ContentView
                let filter = Filter(fromDate: "10/07/2021", toDate: "12/07/2021")
                onDismiss?(filter)
                presentationMode.wrappedValue.dismiss()
            }, label: {
                Text("Applay Filters")
            }).padding()
        }.padding()
    }
}

struct Filter {
    var fromDate: String
    var toDate: String
}
Jessy
  • 157
  • 1
  • 10

1 Answers1

15

You can use @Binding for that (or @StateObject, @ObservedObject, @Environmentobject with @ObservableObject using MVVM Design Pattern)

The code below is an example using @Binding.

Added/Edited Lines

Text("\(filter.fromDate) and \(filter.toDate)") // to see the changed values

@State var filter = Filter(fromDate: "", toDate: "") // in ContentView

@Binding var filter: Filter // in FilterView

FilterView(filter: $filter) // $ used for @Binding parameter

Full Code

struct ContentView: View {
    @State var showModel = false
    @State var filter = Filter(fromDate: "", toDate: "")
    
    var body: some View {
        VStack {
            Text("\(filter.fromDate) and \(filter.toDate)")
            Button(action: {
                showModel.toggle()
            }, label: {
                Text("Show filters")
            }).sheet(isPresented: $showModel, content: {
                FilterView(filter: $filter)
            })
        }
    }
}

struct FilterView: View {
    @Environment(\.presentationMode) var presentationMode

    var onDismiss: ((_ model: Filter) -> Void)?
    @Binding var filter: Filter

    var body: some View {
        
        VStack {
            Button(action: {
                // Pass data from here to ContentView
                filter = Filter(fromDate: "10/07/2021", toDate: "12/07/2021")
                onDismiss?(filter)
                presentationMode.wrappedValue.dismiss()
            }, label: {
                Text("Applay Filters")
            }).padding()
        }.padding()
    }
}

struct Filter {
    var fromDate: String
    var toDate: String
}

enter image description here

enter image description here

enter image description here

Taeeun Kim
  • 964
  • 1
  • 8
  • 20
  • 1
    thanks @Taeeun onDismiss we can the date```swift .sheet(isPresented: $showModel, onDismiss: { print("\(filter.fromDate) and \(filter.toDate)") }, content: { FilterView(filter: $filter) })''' – Jessy Jul 16 '21 at 12:26
  • Any other ways? We have to create object right? `@State var filter: Filter(fromDate: "", toDate: "") ` – Jessy Jul 16 '21 at 12:28
  • @iOSDeveloper you are welcome! Can I ask you again, what do you mean? If you want to create object without initial values like `@State var filter: Filter()`, then you can use Optional(use `?` at the end of type) like `var fromDate: String?` and `var toDate: String?`. Then you can use `@State var filter: Filter()` And you have to change this `filter.fromDate` to `filter.fromDate ?? "no date"`, because it's now Optional value. (or `filter.fromDate!`) – Taeeun Kim Jul 16 '21 at 12:39
  • 3
    What is the use of ```onDismiss``` closure? – Vinod Jun 03 '22 at 10:22
  • @Vinod none. that's his logic. the filter variable getting assigned a value is enough – chitgoks Jan 01 '23 at 07:35