将选定的 DatePicker 传递给另一个视图
Pass a selected DatePicker to another View
我在 mi ContentView 中有一个 DatePicker。
我工作的应用程序要求所有其他视图具有相同的 DatePicker,并在 ContentView 页面中选择了确切的日期。
有什么想法吗?
这里是展示如何使用绑定将 Date
变量传递给其他视图的演示代码。
如果您不需要其他视图更改日期,
然后使用 @State var sameDate: Date
而不是 @Binding var sameDate: Date
.
您也可以使用单个 ObservableObject
并使用 @EnvironmentObject
传递它来实现相同的效果,请参阅 EDIT-1。
import SwiftUI
@main
struct TestApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
struct ContentView: View {
@State var birthDate = Date()
var body: some View {
NavigationView {
VStack (spacing: 80) {
DatePicker(selection: $birthDate, displayedComponents: .date) {
Text("Select a date")
}.padding(.top, 50)
NavigationLink("Next view", destination: NextView1(sameDate: $birthDate))
}
}.navigationViewStyle(.stack)
}
}
struct NextView1: View {
@Binding var sameDate: Date
var body: some View {
VStack (spacing: 80) {
DatePicker(selection: $sameDate, displayedComponents: .date) {
Text("Next view-1 date")
}.padding(.top, 50)
NavigationLink("Next view", destination: NextView2(sameDate: $sameDate))
}
}
}
struct NextView2: View {
@Binding var sameDate: Date
var body: some View {
DatePicker(selection: $sameDate, displayedComponents: .date) {
Text("Next view-2 date")
}.padding(.top, 50)
}
}
EDIT-1:使用 ObservableObject
.
class DateManager: ObservableObject {
@Published var birthDate = Date()
}
struct ContentView: View {
@StateObject var dateManager = DateManager()
var body: some View {
NavigationView {
VStack (spacing: 80) {
DatePicker(selection: $dateManager.birthDate, displayedComponents: .date) {
Text("Select a date")
}.padding(.top, 50)
NavigationLink("Next view 1", destination: NextView1())
}
}.navigationViewStyle(.stack)
.environmentObject(dateManager)
}
}
struct NextView1: View {
@EnvironmentObject var dateManager: DateManager
var body: some View {
VStack (spacing: 80) {
DatePicker(selection: $dateManager.birthDate, displayedComponents: .date) {
Text("Next view-1 date")
}.padding(.top, 50)
NavigationLink("Next view 2", destination: NextView2())
}
}
}
struct NextView2: View {
@EnvironmentObject var dateManager: DateManager
var body: some View {
DatePicker(selection: $dateManager.birthDate, displayedComponents: .date) {
Text("Next view-2 date")
}.padding(.top, 50)
}
}
我在 mi ContentView 中有一个 DatePicker。
我工作的应用程序要求所有其他视图具有相同的 DatePicker,并在 ContentView 页面中选择了确切的日期。
有什么想法吗?
这里是展示如何使用绑定将 Date
变量传递给其他视图的演示代码。
如果您不需要其他视图更改日期,
然后使用 @State var sameDate: Date
而不是 @Binding var sameDate: Date
.
您也可以使用单个 ObservableObject
并使用 @EnvironmentObject
传递它来实现相同的效果,请参阅 EDIT-1。
import SwiftUI
@main
struct TestApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
struct ContentView: View {
@State var birthDate = Date()
var body: some View {
NavigationView {
VStack (spacing: 80) {
DatePicker(selection: $birthDate, displayedComponents: .date) {
Text("Select a date")
}.padding(.top, 50)
NavigationLink("Next view", destination: NextView1(sameDate: $birthDate))
}
}.navigationViewStyle(.stack)
}
}
struct NextView1: View {
@Binding var sameDate: Date
var body: some View {
VStack (spacing: 80) {
DatePicker(selection: $sameDate, displayedComponents: .date) {
Text("Next view-1 date")
}.padding(.top, 50)
NavigationLink("Next view", destination: NextView2(sameDate: $sameDate))
}
}
}
struct NextView2: View {
@Binding var sameDate: Date
var body: some View {
DatePicker(selection: $sameDate, displayedComponents: .date) {
Text("Next view-2 date")
}.padding(.top, 50)
}
}
EDIT-1:使用 ObservableObject
.
class DateManager: ObservableObject {
@Published var birthDate = Date()
}
struct ContentView: View {
@StateObject var dateManager = DateManager()
var body: some View {
NavigationView {
VStack (spacing: 80) {
DatePicker(selection: $dateManager.birthDate, displayedComponents: .date) {
Text("Select a date")
}.padding(.top, 50)
NavigationLink("Next view 1", destination: NextView1())
}
}.navigationViewStyle(.stack)
.environmentObject(dateManager)
}
}
struct NextView1: View {
@EnvironmentObject var dateManager: DateManager
var body: some View {
VStack (spacing: 80) {
DatePicker(selection: $dateManager.birthDate, displayedComponents: .date) {
Text("Next view-1 date")
}.padding(.top, 50)
NavigationLink("Next view 2", destination: NextView2())
}
}
}
struct NextView2: View {
@EnvironmentObject var dateManager: DateManager
var body: some View {
DatePicker(selection: $dateManager.birthDate, displayedComponents: .date) {
Text("Next view-2 date")
}.padding(.top, 50)
}
}