从使用 MessageKit 创建的 ChatViewController 推送其他 ViewController

Posted

技术标签:

【中文标题】从使用 MessageKit 创建的 ChatViewController 推送其他 ViewController【英文标题】:Push on other ViewController from ChatViewController created with MessageKit 【发布时间】:2020-01-04 15:07:54 【问题描述】:

我使用MessageKit 在我的App 中创建Chat

在我以编程方式创建的ChatViewController 中,我添加了rightBarButtonItemuser picture。我想添加 func 如果其他用户点击rightBarButtonItem,它将重定向到UserProfileViewController

现在看起来像这样。用户图片专用蓝色带箭头。

现在我有了这个代码:

class ChatsViewController: MessagesViewController 

    override func viewDidLoad() 
        super.viewDidLoad()
        maintainPositionOnKeyboardFrameChanged = true
        messageInputBar.sendButton.setImage(#imageLiteral(resourceName: "send"), for: .normal)
        messageInputBar.sendButton.title = nil

        messageInputBar.inputTextView.placeholder = NSLocalizedString("New message", comment: "Новое сообщение")

        messageInputBar.delegate = self
        messagesCollectionView.messagesDataSource = self
        messagesCollectionView.messagesLayoutDelegate = self
        messagesCollectionView.messagesDisplayDelegate = self
        messagesCollectionView.messageCellDelegate = self
    

    private func loadChat(channel: Channel) 
        guard let id = channel.id else 
            navigationController?.popViewController(animated: true)
            return
        

        reference = db.collection([FSCollections.chats, id, "thread"].joined(separator: "/"))

        messageListener = reference?.addSnapshotListener  querySnapshot, error in
            guard let snapshot = querySnapshot else 
                print("Error listening for channel updates: \(error?.localizedDescription ?? "No error")")
                return
            

            if UserDefaults.standard.bool(forKey: UserDefaultsKeys.switcherIsClick) == true 
                self.navigationItem.title = self.channel?.recipientName ?? ""
             else 
                self.navigationItem.title = self.channel?.name ?? ""
            
            self.configurePhotoProfile()

            snapshot.documentChanges.forEach  change in
                self.handleDocumentChange(change)
            
        
    

    func configurePhotoProfile() 
        let imageView = UIImageView()
        let widthConstraint = imageView.widthAnchor.constraint(equalToConstant: 34)
        let heightConstraint = imageView.heightAnchor.constraint(equalToConstant: 34)
        let tap = UITapGestureRecognizer(target: self, action: #selector(ChatsViewController.didTapImageButton(_:)))
        tap.numberOfTouchesRequired = 1
        heightConstraint.isActive = true
        widthConstraint.isActive = true
        imageView.kf.setImage(with: URL(string: channel?.recipientUrlImage ?? ""))
        imageView.backgroundColor = .clear
        imageView.layer.masksToBounds = true
        imageView.layer.cornerRadius = 17
        imageView.isUserInteractionEnabled = true
        imageView.addGestureRecognizer(tap)
        if UserDefaults.standard.integer(forKey: UserDefaultsKeys.selectedSegmentedControl) == 0 
            self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: imageView)
         else 
            self.navigationItem.rightBarButtonItem = nil
        
    

    @objc func didTapImageButton(_ sender: UIBarButtonItem) 

        if let detailPhotographerVC = storyboard?.instantiateViewController(withIdentifier: "detailPhotographerVC") as? DetailPhotographerViewController 
            detailPhotographerVC.id = channel?.recipientID ?? ""
            let backItem = UIBarButtonItem()
            backItem.title = ""
            self.navigationItem.backBarButtonItem = backItem
            self.navigationController?.pushViewController(detailPhotographerVC, animated: true)
         else 
            print("error")
        
    

因此,如果用户点击user picture,我会在来自func didTapImageButton控制台 中收到错误消息。

我不明白为什么会出错...在detailPhotographerVC 我重定向正确iddetailPhotographerVC 不能接受除id 以外的任何数据。

UPD。

我尝试在func didTapImageButton 更新我的代码:

@objc func didTapImageButton(_ sender: UIBarButtonItem) 
    let detailPhotographerVC = storyboard!.instantiateViewController(withIdentifier: "detailPhotographerVC") as! DetailPhotographerViewController
    detailPhotographerVC.id = channel?.recipientID ?? ""
    let backItem = UIBarButtonItem()
    backItem.title = ""
    self.navigationItem.backBarButtonItem = backItem
    self.navigationController?.pushViewController(detailPhotographerVC, animated: true)

但又报错,请查看截图。

【问题讨论】:

我在 func didTapImageButton 中得到 print("error"),而在 detailPhotographerVC 上没有重定向 删除 if 并尝试下面的行,如果你得到 1 告诉什么崩溃? 【参考方案1】:

以编程方式加载 vc

let detailPhotographerVC =  DetailPhotographerViewController()

来自故事板

let detailPhotographerVC = storyboard!.instantiateViewController(withIdentifier: "detailPhotographerVC") as! DetailPhotographerViewController 

【讨论】:

没有帮助我,请检查更新问题 你有一个名为“Main”的故事板吗? 哦,不,但我尝试添加它,它对我有帮助,非常感谢!:)

以上是关于从使用 MessageKit 创建的 ChatViewController 推送其他 ViewController的主要内容,如果未能解决你的问题,请参考以下文章

在 MessageKit 中 configureAvatarView 无法正常工作

MessageKit - 当我点击 inputBar 时,键盘不显示

消息线程`MessageKit`swift的左对齐

使用 MessageKit 构建的聊天视图永远不会滚动到带有图片的最后一条消息

MessageKit:添加的输入栏按钮仅在我发送消息后出现

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