微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用for循环获取UNNotificationRequest数组以填充SwiftUI列表视图?

如何解决如何使用for循环获取UNNotificationRequest数组以填充SwiftUI列表视图?

我想简单地获取已调度的本地通知列表,并使用forEach填充SwiftUI列表。我相信它应该像我在下面所做的那样工作,但是该数组始终为空,因为似乎在for循环完成之前就使用了它。我尝试将getNotifications()函数与完成处理程序一起使用,也将其作为返回函数使用,但两种方法仍然无效。如何等待for循环完成以填充列表?或者,如果还有另一种方法,请告诉我,谢谢。

var notificationArray = [UNNotificationRequest]()

func getNotifications() {
        
print("getNotifications")
        center.getPendingNotificationRequests(completionHandler: { requests in
            for request in requests {
                print(request.content.title)
                notificationArray.append(request)
            }
        })

}

struct ListView: View {

var body: some View {
    
    NavigationView {
    List {
        ForEach(notificationArray,id: \.content) { notification in

            HStack {
                vstack(alignment: .leading,spacing: 10) {
                    let notif = notification.content
                    Text(notif.title)
                    Text(notif.subtitle)
                    .opacity(0.5)
            }
                    }
        
    }

}
 .onAppear() {
        getNotifications()
    }
}
}

更新:

这是我添加通知并再次致电getNotifications的方式。我希望列表随着新数组的创建而动态更新。打印到控制台显示getNotifications正常工作,并且新数组包含添加通知

Section {
                Button(action: {
                    print("Adding Notification: ",title,bodyText,timeIntValue[previewIndex])
                    addNotification(title: title,bodyText: bodyText,timeInt: timeIntValue[previewIndex])
                    showDetail = false
                    self.vm.getNotifications()
                }) {
                    Text("Save Notification")
                }
            }.disabled(title.isEmpty || bodyText.isEmpty)

解决方法

视图未观察到您的全局notificationArray。应该是动态属性...可能的解决方案是将其包装到ObservableObject视图模型中。

这是解决方案的演示:

class ViewModel: ObservableObject {
  @Published var notificationArray = [UNNotificationRequest]()

  func getNotifications() {
        
    print("getNotifications")
        center.getPendingNotificationRequests(completionHandler: { requests in
            var newArray = [UNNotificationRequest]()
            for request in requests {
                print(request.content.title)
                newArray.append(request)
            }
            DispatchQueue.main.async {
              self.notificationArray = newArray
            }
        })
  }
}

struct ListView: View {
  @ObservedObject var vm = ViewModel()
//@StateObject var vm = ViewModel()    // << for SwiftUI 2.0

  var body: some View {
    
    NavigationView {
      List {
        ForEach(vm.notificationArray,id: \.content) { notification in
            HStack {
                VStack(alignment: .leading,spacing: 10) {
                    let notif = notification.content
                    Text(notif.title)
                    Text(notif.subtitle)
                    .opacity(0.5)
                }
            }
      }
   }
   .onAppear() {
        self.vm.getNotifications()
    }
 }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。