SwiftUI 如何在关闭时将数据传递到上一个屏幕
SwiftUI how to pass data to previous screen on dismiss
我想将关闭 presentViewController
的数据传递到上一个屏幕。在这里,我想使用块将数据作为 UIKitApp 传递到上一个屏幕。但我不知道要传递数据。 options
我们必须向后传递的数据是什么?
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
}
您可以为此使用 @Binding
(或使用 MVVM 设计模式 @StateObject
、@ObservedObject
、@Environmentobject
和 @ObservableObject
)
下面的代码是使用@Binding 的示例。
Added/Edited 行
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
完整代码
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
}
我想将关闭 presentViewController
的数据传递到上一个屏幕。在这里,我想使用块将数据作为 UIKitApp 传递到上一个屏幕。但我不知道要传递数据。 options
我们必须向后传递的数据是什么?
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
}
您可以为此使用 @Binding
(或使用 MVVM 设计模式 @StateObject
、@ObservedObject
、@Environmentobject
和 @ObservableObject
)
下面的代码是使用@Binding 的示例。
Added/Edited 行
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
完整代码
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
}