我正在尝试在计时器完成后向我的应用添加警报,我只想要一个弹出窗口来说明计时器基本上已完成。每当我尝试任何事情时,我都会收到此错误“警报”初始化程序的结果未使用。
感谢任何帮助。
constructor {instanceVar} {
namespace upvar [namespace current] instanceVar iv
set iv $instanceVar
}
答案 0 :(得分:2)
"onReceive" 不是显示警报的正确位置。使用“.alert(...)”显示它, 例如在此测试代码中:
import SwiftUI
@main
struct TestApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
struct ContentView: View {
@State private var showAlert = false
var body: some View {
Button("show alert") {
showAlert.toggle()
}
.alert(isPresented: $showAlert) {
Alert(title: Text("This is a test"), message: Text("This is a test message!"))
}
}
}
因此,使用“onReceive”中的计时器,只需使用“showAlert”作为测试按钮。类似的东西:
.onReceive(timerMin) { _ in
guard isActive else { return }
if timeRemainingMin > 0 {
timeRemainingMin -= 1
} else {
isActive = false
timeRemainingMin = defaultTimeRemainingMin
showAlert = true
}
}