【发布时间】:2021-04-10 17:43:52
【问题描述】:
我对 Swift 很陌生。出于学习目的,我正在创建一个小倒计时应用程序(距离 X 日期还有几天)。 这些倒计时显示在列表中。列表中的单个倒计时由以下视图表示。
struct CountdownRow: View {
@State private var remainingDays: Int = 0
@State private var progress: Float = 0.0
var countdown: Countdown
var body: some View {
VStack{
HStack {
Text(countdown.name)
.font(.subheadline)
.multilineTextAlignment(.leading)
.padding(10)
Spacer()
Text(String(remainingDays))
.font(.headline)
.padding(10)
}
ProgressView(value: progress)
.padding(10)
}
.onAppear {
remainingDays = countdown.getRemainingDays()
progress = countdown.getProgress()
}
.onReceive(NotificationCenter.default.publisher(for:
UIApplication.significantTimeChangeNotification),
perform: { _ in
remainingDays = countdown.getRemainingDays()
progress = countdown.getProgress()
})
}
}
我想在午夜更新每个倒计时的剩余天数。但是当时间从 23:59 变为 0:00 时,.onReceive 修饰符不会触发。 接收其他通知(例如 UIApplication.willEnterForegroundNotification)会按预期工作。
为什么没有在午夜发出(或接收)此特定通知? 根据苹果的文档,它应该在午夜发出。
谢谢!
【问题讨论】: