我一直在尝试使用swiftUI并查看了这个Ray Wenderlich教程... 我注意到他们没有重新实现"下一轮"功能... 所以我试着自己做。遇到了一个问题(也许他们也遇到了):
基本问题更普遍: 使用swiftUI,当Alert被解除时(当用户点击"确定"时),如何触发函数? 我尝试使用Alert构造函数的dismissButton参数...
(还有View的.onDisappear方法,但我不知道如何将其应用于Alert视图。)
代码:
基本问题更普遍: 使用swiftUI,当Alert被解除时(当用户点击"确定"时),如何触发函数? 我尝试使用Alert构造函数的dismissButton参数...
(还有View的.onDisappear方法,但我不知道如何将其应用于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)
*/
})
}
}