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

问题描述 投票:0回答:1

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

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

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

enter image description here

现在我有了这个代码:

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收到console的错误。

我不明白为什么会出现错误...在detailPhotographerVC上,我重定向了正确的id,并且detailPhotographerVC不得接受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)
}

但是再次出现错误,请检查屏幕截图。

enter image description here

ios swift xcode pushviewcontroller messagekit
1个回答
0
投票

以编程方式加载vc

let detailPhotographerVC =  DetailPhotographerViewController()

来自故事板

let detailPhotographerVC = storyboard!.instantiateViewController(withIdentifier: "detailPhotographerVC") as! DetailPhotographerViewController 
© www.soinside.com 2019 - 2024. All rights reserved.