I use MessageKit
for create Chat
in my App
.
In ChatViewController
which i created programmatically I added rightBarButtonItem
with user picture
. I want to add func which will be redirect on UserProfileViewController
if other user hits on rightBarButtonItem
.
Now it looks like this. User picture dedicated blue color with arrows.
Now I have this code:
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")
}
}
}
So, if user tap on user picture
I get an error in console from func didTapImageButton
.
I don't understand why I get an error... On detailPhotographerVC
I redirect correct id
and detailPhotographerVC
must not accept any data other than id.
UPD.
I try update my code in func didTapImageButton
on:
@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)
}
But again get an error, check screenshot.
Load vc programmatically
let detailPhotographerVC = DetailPhotographerViewController()
from storyboard
let detailPhotographerVC = storyboard!.instantiateViewController(withIdentifier: "detailPhotographerVC") as! DetailPhotographerViewController