class 中函数的 SwiftUI 简单弹出警报
SwiftUI simple Pop-Up Alert from a function in a class
我有点困惑,我尝试在我的功能为真时在我的视图中显示一个弹出窗口,否则继续,但我不知道该怎么做我在不同的做事方式之间迷失了方向。我尝试了几件事,但有些事情让我逃脱了。
编辑:该项目基于SwiftUI 和弹窗显示在SwiftUI 视图中,函数在class 返回一个ObservableObject。该函数是通过按钮从 SwiftUI 视图调用的。
get_deliveries.swift -> 变成 class delivViewModel:ObservableObject
func cancelDeliv(delivID: String) {
let delivRef = db.collection("av_deliveries").document(delivID)
let isDemain = Date().dayAfter.toString()
delivRef.getDocument { (document, error) in
if let document = document, document.exists {
let property = document.get("when")
if isDemain == property as! String {
// Print a pop-up error
} else {
delivRef.updateData([
"taken": false,
"taken_by": ""
]) { err in
if let err = err {
print("Error updating document: \(err)")
} else {
print("Document successfully updated")
}
}
}
}
}
}
这是一个基本演示,它通过 ObservableObject
class.
的视图主体中的按钮激活警报
struct ContentView: View {
// However you've initialized your class, it may be different than this.
@StateObject var progress = DeliveriesViewModel()
var body: some View {
VStack {
// Other views in your View body
// SwiftUI button to activate the Bool in ObservableObject class.
Button {
progress.isDisplayingAlert.toggle()
} label: {
Text("Toggle Alert")
}
}
.alert(isPresented: $progress.isDisplayingAlert) { () -> Alert in
Alert(title: Text("Alert Title"), message: Text("Body of Alert."), dismissButton: .cancel())
}
}
}
class DeliveriesViewModel: ObservableObject {
@Published var isDisplayingAlert = false
func displayAlert() {
// other function actions...
// update Published property.
isDisplayingAlert = true
// other function actions...
}
}
我有点困惑,我尝试在我的功能为真时在我的视图中显示一个弹出窗口,否则继续,但我不知道该怎么做我在不同的做事方式之间迷失了方向。我尝试了几件事,但有些事情让我逃脱了。
编辑:该项目基于SwiftUI 和弹窗显示在SwiftUI 视图中,函数在class 返回一个ObservableObject。该函数是通过按钮从 SwiftUI 视图调用的。
get_deliveries.swift -> 变成 class delivViewModel:ObservableObject
func cancelDeliv(delivID: String) {
let delivRef = db.collection("av_deliveries").document(delivID)
let isDemain = Date().dayAfter.toString()
delivRef.getDocument { (document, error) in
if let document = document, document.exists {
let property = document.get("when")
if isDemain == property as! String {
// Print a pop-up error
} else {
delivRef.updateData([
"taken": false,
"taken_by": ""
]) { err in
if let err = err {
print("Error updating document: \(err)")
} else {
print("Document successfully updated")
}
}
}
}
}
}
这是一个基本演示,它通过 ObservableObject
class.
struct ContentView: View {
// However you've initialized your class, it may be different than this.
@StateObject var progress = DeliveriesViewModel()
var body: some View {
VStack {
// Other views in your View body
// SwiftUI button to activate the Bool in ObservableObject class.
Button {
progress.isDisplayingAlert.toggle()
} label: {
Text("Toggle Alert")
}
}
.alert(isPresented: $progress.isDisplayingAlert) { () -> Alert in
Alert(title: Text("Alert Title"), message: Text("Body of Alert."), dismissButton: .cancel())
}
}
}
class DeliveriesViewModel: ObservableObject {
@Published var isDisplayingAlert = false
func displayAlert() {
// other function actions...
// update Published property.
isDisplayingAlert = true
// other function actions...
}
}