小编X0r*_*0rc的帖子

为什么没有发出(或没有收到)significantTimeChangeNotification?

我对斯威夫特很陌生。出于学习目的,我正在创建一个小倒计时应用程序(距离日期 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()
    })
  }
}
Run Code Online (Sandbox Code Playgroud)

我想在午夜更新每个倒计时的剩余天数。但当时间从 23:59 变为 0:00 时,.onReceive 修饰符不会触发。接收其他通知(例如 UIApplication.willEnterForegroundNotification)则按预期工作。

为什么在午夜没有发出(或接收)此特定通知?根据苹果的文档,它应该在午夜发出。

谢谢你!

swift swiftui combine

4
推荐指数
1
解决办法
771
查看次数

标签 统计

combine ×1

swift ×1

swiftui ×1