Я пробовал использовать swiftUI и просмотрел этот учебник Рэя Вендерлиха ... Я заметил, что они не повторно реализовали функциональность "nextRound" ... поэтому я попытался сделать это сам. Возникла проблема (что, возможно, и они сделали):
Основной вопрос более общий:
Используя swiftUI, как вы запускаете функцию, когда предупреждение закрывается - когда пользователь нажимает "ОК". ?
Я пробовал использовать аргумент dismissButton конструктора Alert ...
(а также метод .onDisappear View, но я не могу понять, как применить его к представлению Alert.)
Код:
import SwiftUI
struct ContentView: View {
@State var shouldShowAlert: Bool = false
// this never gets called
func onAlertDismissed() {
print("you will not see this in the console")
}
// this doesn't seem to work
var dismissButton: some View {
Button(action: {
self.onAlertDismissed()
}) {
// Bilbo Baggins does not appear -- "OK" still shows
Text("BILBO BAGGINS")
}
}
var body: some View {
VStack {
Spacer()
Button(action: {
self.shouldShowAlert = true
}) {
Text("show the alert!")
}
Spacer()
}.alert(isPresented: $shouldShowAlert, content: {
// what to add here?
Alert(title: Text("Alert:"), message: Text("press OK to execute onAlertDismissed()..."))
// what I have tried and doesn't work:
/*
Alert(title: Text("Alert:"), message: Text("press OK to execute onAlertDismissed()..."), dismissButton: self.dismissButton as? Alert.Button)
*/
})
}
}