ios - 当用户使用 'MessageKit' 库点击发送时,监听器不会触发

标签 ios xcode messagekit

我正在尝试实现一个简单的消息应用程序,但 MessageInputBarDelegate 方法未触发。其他扩展(例如 MessagesDisplayDelegate)似乎会按预期触发。

我使用的是 Swift 4.4.2

pod 文件:

use_frameworks!

target 'm-v0.01' do
    pod 'Socket.IO-Client-Swift', '~> 15.1.0'
    pod 'Alamofire', '~> 5.0.0-beta.5'
    pod 'MessageKit'
#    pod 'MessageInputBar'
#    pod 'MessageInputBar', :git => 'https://github.com/MessageKit/MessageInputBar.git', :branch => 'master'
end 

我有两个 Controller ,一个用于登录,一个用于显示消息:

ViewController:(用于登录)

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var loginValue: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?)
    {
        if segue.destination is MViewController
        {
            let vc = segue.destination as? MViewController
            vc?.text = loginValue.text!
        }
    }

    @IBAction func loginAction(_ sender: UIButton!) {

        print("Entered Text" , loginValue.text)



        performSegue(withIdentifier: "view2", sender: self)

    }

}

MViewController(用于显示消息):

import UIKit
import SocketIO
import Alamofire
import MessageKit

extension MViewController: MessagesLayoutDelegate {
    func heightForLocation(message: MessageType,
                           at indexPath: IndexPath,
                           with maxWidth: CGFloat,
                           in messagesCollectionView: MessagesCollectionView) -> CGFloat {

        return 0
    }
}

extension MViewController: MessagesDisplayDelegate {
    func configureAvatarView(
        _ avatarView: AvatarView,
        for message: MessageType,
        at indexPath: IndexPath,
        in messagesCollectionView: MessagesCollectionView) {

        let message = messages[indexPath.section]
        let color = message.member.color
        avatarView.backgroundColor = color

        print("MessagesDisplayDelegate")
    }
}

extension MViewController: MessageCellDelegate {

    func didTapAvatar(in cell: MessageCollectionViewCell) {
        print("Avatar tapped")
    }

    func didTapMessage(in cell: MessageCollectionViewCell) {
        print("Message tapped")

    }

    func didTapCellTopLabel(in cell: MessageCollectionViewCell) {
        print("Top cell label tapped")
    }

    func didTapMessageTopLabel(in cell: MessageCollectionViewCell) {
        print("Top message label tapped")
    }

    func didTapMessageBottomLabel(in cell: MessageCollectionViewCell) {
        print("Bottom label tapped")
    }

    func didTapAccessoryView(in cell: MessageCollectionViewCell) {
        print("Accessory view tapped")
    }

}


extension MViewController: MessageInputBarDelegate {

    func messageInputBar(_ inputBar: MessageInputBar, didPressSendButtonWith text: String) {
        print("here")
    }

    func messageInputBar(_ inputBar: MessageInputBar, textViewTextDidChangeTo text: String) {
        print("changed")
    }

}

extension MViewController: MessagesDataSource {

    func currentSender() -> SenderType {
        return Sender(id: member.name, displayName: member.name)
    }

    func numberOfSections(
        in messagesCollectionView: MessagesCollectionView) -> Int {
        return messages.count
    }

    func messageForItem(
        at indexPath: IndexPath,
        in messagesCollectionView: MessagesCollectionView) -> MessageType {

        return messages[indexPath.section]
    }

    func messageTopLabelHeight(
        for message: MessageType,
        at indexPath: IndexPath,
        in messagesCollectionView: MessagesCollectionView) -> CGFloat {

        return 12
    }

    func messageTopLabelAttributedText(
        for message: MessageType,
        at indexPath: IndexPath) -> NSAttributedString? {

        return NSAttributedString(
            string: message.sender.displayName,
            attributes: [.font: UIFont.systemFont(ofSize: 12)])
    }
}

class MViewController: MessagesViewController{

    var text:String = ""

    var messages: [Message] = []
    var member: Member!

    override func viewDidLoad() {
        super.viewDidLoad()
        messagesCollectionView.messagesDataSource = self
        messagesCollectionView.messagesLayoutDelegate = self
        messagesCollectionView.messagesDisplayDelegate = self
        messagesCollectionView.messageCellDelegate = self
        messageInputBar.delegate = self
    }

}

我查看了许多教程和文档,这始终被记录为访问当用户点击发送或更改文本时触发的方法:

func messageInputBar(_ inputBar: MessageInputBar, didPressSendButtonWith text: String) {
    print("here")
}

func messageInputBar(_ inputBar: MessageInputBar, textViewTextDidChangeTo text: String) {
    print("changed")
}

我在发送消息的方式中是否遗漏了一些阻止消息监听器被触发的内容?

有一个 pod 库'MessageInputBar',我也应该利用它吗?看来我在代码编译时不需要这个?

更新:

我尝试在 messagesCollectionView 上设置委托(delegate):

 messagesCollectionView.delegate = self as MessageInputBarDelegate as? UICollectionViewDelegate

但结果相同:发送事件未触发。

最佳答案

根据评论中的信息,我假设您没有消息栏的导出,并且没有为 viewController 设置委托(delegate)。尝试在 viewDidLoad 中设置它:

messageInputBar.delegate = self

为此,您的 Controller 必须子类化 MessagesViewController,因此在您的情况下它将发生变化

class ViewController: UIViewController {

到:

class ViewController: MessagesViewController {

关于ios - 当用户使用 'MessageKit' 库点击发送时,监听器不会触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56756706/

相关文章:

iphone - 捕获 dealloc(释放)错误

ios - NSMutableParagraphStyle 未被调用

swift - Storyboard 未链接到 ViewController.swift Xcode 中的 @IBAction/Outlet

ios - 是否可以让我的类(class)遵守这个似乎需要结构的协议(protocol)?

ios - 如何使用 MessageKit 创建自定义单元格?

ios - 如何解码光盘上应用程序文档文件夹的模拟器路径?

ios - iOS 中的触摸点

Xcode 项目图标文件和 Organizer Archives 图稿

ios - Xcode 目标 : Images from main target not loading in test target

ios - 添加对自定义单元格的支持后,MessageKit Collection View 不显示任何单元格