我希望用户在按下键盘上的“发送”按钮时也能够发送消息。对于 textField,我知道可以通过 delagte 来实现。但是谁能指出我如何在 MessageKit 中做到这一点?我无法在这里找到合适的方法。提前致谢!
问问题
42 次
1 回答
0
可以在 repo 上的示例项目中找到详细说明。MessageKit
实现在发送按钮点击上发送消息的代码片段如下所示:
import UIKit
import MessageKit
import InputBarAccessoryView
class MyViewController: MessagesViewController {
override func viewDidLoad() {
super.viewDidLoad()
messageInputBar.delegate = self //set the delegate to receive notifications from the `InputBarAccessoryView`
}
}
extension MyViewController: InputBarAccessoryViewDelegate {
@objc internal func inputBar(_ inputBar: InputBarAccessoryView, didPressSendButtonWith text: String) {
processInputBar(messageInputBar)
}
private func processInputBar(_ inputBar: InputBarAccessoryView) {
let components = inputBar.inputTextView.components
inputBar.inputTextView.text = String()
inputBar.invalidatePlugins()
inputBar.inputTextView.resignFirstResponder() // Resign first responder for iPad split view
DispatchQueue.global(qos: .default).async {
DispatchQueue.main.async { [weak self] in
guard let self = self else {return}
self.insertMessages(components)
self.messagesCollectionView.scrollToLastItem(animated: true)
}
}
}
private func insertMessages(_ data: [Any]) {
for component in data {
if let string = component as? String {
// Create a Message type and add it the the chat messages
// let message = Message(sender: currentUser, messageId: UUID().uuidString, kind: .text(string))
}
else if let image = component as? UIImage {
let item = ImageMediaItem(image: image)
// Create a Message type and add it the the chat messages
// let message = Message(sender: currentUser, messageId: UUID().uuidString, kind: .photo(item))
}
}
}
}
于 2022-02-04T14:06:56.403 回答