gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 05:12:01 25 4
gpt4 key购买 nike

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

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

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

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

因此,如果用户点击用户图片,我会在func didTapImageButton控制台中收到错误。

我不明白为什么会收到错误...在 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

最佳答案

以编程方式加载 vc

let detailPhotographerVC =  DetailPhotographerViewController()

来自 Storyboard

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

关于ios - 从使用 MessageKit 创建的 ChatViewController 推送其他 ViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59591962/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com