SwiftUI how to pass data to previous screen on dismiss
Asked Answered
G

1

9

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
}
Gaskins answered 16/7, 2021 at 11:59 Comment(0)
G
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

Gord answered 16/7, 2021 at 12:14 Comment(6)
thanks @Taeeun onDismiss we can the date```swift .sheet(isPresented: $showModel, onDismiss: { print("(filter.fromDate) and (filter.toDate)") }, content: { FilterView(filter: $filter) })'''Gaskins
Any other ways? We have to create object right? @State var filter: Filter(fromDate: "", toDate: "") Gaskins
@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!)Gord
What is the use of onDismiss closure?Huppah
@Huppah none. that's his logic. the filter variable getting assigned a value is enoughNereus
Hi, I didn't get the onDismiss closure? Would you kindly provide more details? @TaeeunKimOrissa

© 2022 - 2024 — McMap. All rights reserved.