如何从 UIKit 向 SwiftUI 中的视图发送通知?
我正在尝试在用户拉动刷新后从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最简单的方法是首先创建自定义通知,如下所示:
现在,您可以使用点符号来处理它。接下来,在您的 VC 中:
最后,在您的 SwiftUI 视图中:
编辑:
如果您想在变量更改时在 SwiftUI 视图中发送消息,那么您可以像这样使用
.onChange(of:)
:The simplest way of doing this would be to first, create the custom notification like this:
That now lets you address it with dot notation. Next, in your VC:
Lastly, in your SwiftUI view:
edit:
If you want to send a message in a SwiftUI view when a variable changed, then you could use
.onChange(of:)
like this: