ios - 从使用 MessageKit 创建的 ChatViewController 推送其他 ViewController
问题描述
我MessageKit
在Chat
我的App
.
在ChatViewController
其中我以编程方式创建我添加rightBarButtonItem
了user picture
. 我想添加funcUserProfileViewController
如果其他用户点击,它将被重定向rightBarButtonItem
。
现在看起来像这样。用户图片专用蓝色带箭头。
现在我有这个代码:
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
我重定向正确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)
}
但再次出现错误,请检查屏幕截图。
解决方案
以编程方式加载 vc
let detailPhotographerVC = DetailPhotographerViewController()
来自故事板
let detailPhotographerVC = storyboard!.instantiateViewController(withIdentifier: "detailPhotographerVC") as! DetailPhotographerViewController
推荐阅读
- linker - 尝试添加自定义依赖项时 Meson 和 Ninja 中的链接器错误
- ruby-on-rails - 无法使用数据库值填充 haml 中的表 - Ruby on Rails
- microstrategy - 如何查询微策略元数据?
- python - 网络x中边缘介数中心性子集的计算
- maven - 通过 jenkins 声明性管道增加 maven 版本
- java - Netty 和 Project Loom
- javascript - 将 DataTable 数据加载到编辑模式
- vue.js - 每当我尝试访问 Vue 时,它都会返回未定义的 vm
- reactjs - 为我的 Web 应用程序设置检查过滤器问题
- java - 如何使 ActivityOptions 在工作流程中像它的生命周期一样动态