如何从 UIKit 向 SwiftUI 中的视图发送通知?

发布于 2025-01-15 02:24:30 字数 543 浏览 0 评论 0原文

我正在尝试在用户拉动刷新后从 UIViewcontroller 向 SwiftUI View 发送通知。

 @objc private func fetchScheduleData(_ sender: UIRefreshControl) {
        NotificationCenter.default.post(name: Notification.Name(rawValue: "didPullToRefreash"), object: nil)
     
    }

在 SwiftUI 视图上,我尝试设置此方法 .onchange()

   NotificationCenter.default.addObserver(self, selector: #selector(didPullToRefreashHelper), name: Notification.Name(rawValue: "didTapNotification"), object: nil)

但 onChange 它不起作用。我想知道我将如何做到这一点。

I am trying to send a notification from UIViewcontroller to SwiftUI View after the user did pull to refresh.

 @objc private func fetchScheduleData(_ sender: UIRefreshControl) {
        NotificationCenter.default.post(name: Notification.Name(rawValue: "didPullToRefreash"), object: nil)
     
    }

On SwiftUI view i trying to set this method .onchange()

   NotificationCenter.default.addObserver(self, selector: #selector(didPullToRefreashHelper), name: Notification.Name(rawValue: "didTapNotification"), object: nil)

But onChange it's not working. I am wondering i how i will do this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怼怹恏 2025-01-22 02:24:30

最简单的方法是首先创建自定义通知,如下所示:

extension Notification.Name {
    static let didPullToRefresh = Notification.Name("didPullToRefresh")
}

现在,您可以使用点符号来处理它。接下来,在您的 VC 中:

 @objc private func fetchScheduleData(_ sender: UIRefreshControl) {
        NotificationCenter.default.post(name: .didPullToRefresh, object: nil)
    }

最后,在您的 SwiftUI 视图中:

.onReceive(NotificationCenter.default.publisher(for: .didPullToRefresh)) { _ in
    // If you are passing an object, this can be "notification in"

    // Do something here as a result of the notification
}

编辑:

如果您想在变量更改时在 SwiftUI 视图中发送消息,那么您可以像这样使用 .onChange(of:) :

.onChange(of: watchedStateVar) { value in
    NotificationCenter.default.post(name: .didPullToRefresh, object: value)
}

The simplest way of doing this would be to first, create the custom notification like this:

extension Notification.Name {
    static let didPullToRefresh = Notification.Name("didPullToRefresh")
}

That now lets you address it with dot notation. Next, in your VC:

 @objc private func fetchScheduleData(_ sender: UIRefreshControl) {
        NotificationCenter.default.post(name: .didPullToRefresh, object: nil)
    }

Lastly, in your SwiftUI view:

.onReceive(NotificationCenter.default.publisher(for: .didPullToRefresh)) { _ in
    // If you are passing an object, this can be "notification in"

    // Do something here as a result of the notification
}

edit:

If you want to send a message in a SwiftUI view when a variable changed, then you could use .onChange(of:) like this:

.onChange(of: watchedStateVar) { value in
    NotificationCenter.default.post(name: .didPullToRefresh, object: value)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文