简体   繁体   English

我们如何检测用户是否使用 MessageKit 按下键盘上的“发送/返回”按钮?

[英]How can we detect if user press the "Send/Return" button on keyboard with MessageKit?

I would like the user to be able to send messages when he/she presses the "Send" button on the keyboard also.我希望用户在按下键盘上的“发送”按钮时也能够发送消息。 For a textField I know that can be achieved with the delagte.对于 textField,我知道可以通过 delagte 来实现。 But could anyone please point me how can we do this in MessageKit?但是谁能指出我如何在 MessageKit 中做到这一点? I was not able to find a proper method to use here.我无法在这里找到合适的方法。 Thanks in advance!提前致谢!

A detailed explanation can be found in the example project on the MessageKit repo.可以在MessageKit库的示例项目中找到详细说明。

The code snippet for implemeting sending the message on a Send button tap, can be seen below:实现在发送按钮点击上发送消息的代码片段如下所示:

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))
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当用户按下键盘上的返回按钮时如何关闭键盘? - How to dismiss keyboard when a user press return button on keyboard? 什么功能可以帮助我了解用户在文本字段中输入的第一个字符,以及按键盘返回键时如何使空白行? - what function can help me to know user input first character in textfield and how to make a blank line when I press keyboard return? 如何检测iPad用户点击键盘隐藏按钮? - How to detect iPad user tap on keyboard hide button? UITextview键盘不允许按下“发送”按钮 - UITextview Keyboard doesn't allow to press “send” button 按下按钮使用户返回初始应用程序状态 - Return user to initial application state on button press 以编程方式按键盘按钮 - Programmatically press a keyboard button 如何检测 flutter 中的硬件按钮按下 - How to detect hardware button press in flutter 当用户点击使用“MessageKit”库发送时,监听器不会触发 - Listener not firing when user hits send using 'MessageKit' library 如何在Cardboard视图中检测到按下后退按钮? - How to detect the press of the back button in Cardboard view? 我们可以检测用户是否通过主页按钮或锁定按钮而不听darwin通知吗? - Can we detect whether a user left through the home button or lock button without listening to darwin notifications?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM