我在.contextMenu
中有一个按钮(即:标记为已付款)
它要做的第一件事是在actionSheet上选择付款方式。立即添加一些代码,以通过EventKit将条目添加到用户日历中。
我的问题是我希望actionSheet返回结果之前,然后执行EventKit代码。
您如何等待它?
属性:
@State private var showPaymentSheet = false
@State private var paymentType = ""
var actionSheet: ActionSheet {
ActionSheet(title: Text("Payment Method"), buttons: [
.default(Text("? Cash")) { paymentType = "? Cash" },
.default(Text("? Bank Transfer")) { paymentType = "? Bank" },
.default(Text("?️ PayPal")) { paymentType = "?️ PayPal" },
.default(Text("? Cheque")) { paymentType = "? Cheque" },
.cancel()
])
}
身体:
.contextMenu {
Button(action: {
**// Run This ActionSheet FIRST and get result of \(paymentType) **
self.showPaymentSheet.toggle()
**// ONLY then continue with below **
let thisDay = self.selectDate.selectedDate
let eventStore = EventsRepository.shared.eventStore
let payment = EKEvent(eventStore: eventStore)
payment.calendar = event.calendar
payment.startDate = event.startDate
payment.endDate = event.endDate
payment.title = "?\(event.title!)"
payment.notes = "\(event.notes!)\nPaid: \(Date().dateType(dateFormat: "EEE MMM dd yyyy @ HH:mm b " )) by \(paymentType)"
payment.location = "\(event.location!)"
do {
try eventStore.save(payment, span: .thisEvent)//i - Save Updated (event)
try EventsRepository.shared.eventStore.remove(event, span: .thisEvent)
EventsRepository.shared.loadAndUpdateEvents(selectedDate: thisDay)
} catch {
print("Failled to cancel event")
}
NotificationCenter.default.post(name: .eventsDidChange, object: nil)
}){
HStack {
Text("Mark as Paid")
.font(Font.custom("ChalkboardSE-Bold", size: 32))
Image("payment")
.renderingMode(.original)
}
}.actionSheet(isPresented: $showPaymentSheet, content: {
self.actionSheet})
}
答案 0 :(得分:0)
您可以使用onReceive
。
这是一个简单的演示:
struct ContentView: View {
@State private var showPaymentSheet = false
@State private var paymentType = ""
var body: some View {
Button("Choose payment") {
self.showPaymentSheet.toggle()
}
.actionSheet(isPresented: $showPaymentSheet) {
self.actionSheet
}
.onReceive(Just(paymentType)) { // <- add here
guard !$0.isEmpty else { return }
self.someOtherFunc()
}
}
var actionSheet: ActionSheet {
...
}
func someOtherFunc() {
print(paymentType)
}
}
但是,我建议将逻辑移到一个单独的类上并保持View clean 。还为什么paymentType = ""
?如果可以未设置,则应该改用nil
。以及为什么您首先需要让它可观察?您还可以为付款类型创建一个单独的枚举。
经过上述修改,我们可以编写一个更新的示例:
enum PaymentType: String {
case cash = "? Cash"
case bank = "? Bank Transfer"
...
}
class PaymentHandler: ObservableObject {
func pay(_ paymentType: PaymentType) {
// print(paymentType.rawValue) // do something with `paymentType`
}
}
struct ContentView: View {
@ObservedObject private var vm = ViewModel()
@State private var showPaymentSheet = false
var body: some View {
Button("Choose payment") {
self.showPaymentSheet.toggle()
}
.actionSheet(isPresented: $showPaymentSheet) {
self.actionSheet
}
}
var actionSheet: ActionSheet {
ActionSheet(title: Text("Payment Method"), buttons: [
.default(Text(PaymentType.cash.rawValue)) { self.vm.pay(.cash) },
.default(Text(PaymentType.bank.rawValue)) { self.vm.pay(.bank) },
.cancel(),
])
}
}