【发布时间】:2020-07-26 19:44:15
【问题描述】:
我正在尝试为 SwiftUI 中 UITextView 的现有子类处理 UIResponder。我已经能够使用协调器模式来处理UITextViewDelegate,但我遇到了UIResponder 的问题。
在过去(使用 UIKit),我会使用 NotificationCenter 在 UIViewController 的子类中为 UIResponder.keyboardWillShowNotification 添加观察者。
在 SwiftUI 中,我不知道该放在哪里。我做了一个简单的事情,就是在makeUIView中重用Coordinator类,如下:
let nc = NotificationCenter.default
nc.addObserver(context.coordinator, selector: #selector(Coordinator.keyboardWillShow(notification:)), name: UIResponder.keyboardWillShowNotification, object: editorTextView)
但是keyboardWillShow 方法永远不会被调用。我做错了吗?
【问题讨论】:
标签: swift swiftui uiresponder