I'm trying to add a feature to my group chat app where if an avatar is tapped on, you will be brought to a profile page which has information about that user. The framework I'm using (JSQMesssagesViewController) has a built in method for tapping the avatar, didTapAvatarImageView
.
override func collectionView(_ collectionView: JSQMessagesCollectionView!, didTapAvatarImageView avatarImageView: UIImageView!, at indexPath: IndexPath!) {
let message = messages[indexPath.item]
DispatchQueue.main.async {
let profileVC = ProfileViewController()
let name = message.senderDisplayName ?? ""
let gender = self.genderDictionary.value(forKey: message.senderId) as? String ?? ""
let status = self.statusDictionary.value(forKey: message.senderId) as? String ?? ""
let avatarString = self.avatarDictionary.value(forKey: message.senderId) as? String ?? ""
print("\n\nsegueTest: name: \(name), gender: \(gender), avatar: \(avatarString), status: \(status)\n\n")
profileVC.profileImageView.loadImageUsingCacheWithUrlString(avatarString)
profileVC.nameLabel.text = name
switch gender {
case "male":
profileVC.nameLabel.textColor = UIColor(hexString: "8097E1")
case "female":
profileVC.nameLabel.textColor = UIColor(hexString: "E98BD5")
default:
profileVC.nameLabel.textColor = UIColor(hexString: "4F4F4F")
}
switch status {
case "2FCB78":
profileVC.profileGoingOutLabel.text = "\(name) wants to go out tonight!"
case "E13F34":
profileVC.profileGoingOutLabel.text = "\(name) is staying in tonight."
default:
profileVC.profileGoingOutLabel.text = "\(name) might want to go out tonight - let them know what the plans are!"
}
self.performSegue(withIdentifier: "chatToProfile", sender: self)
}
}
When I tap on an avatar, I get the unexpectedly found nil crash - but my print statement prints the values with no problem, so they do exist. I'm not sure where I'm going wrong here, as Xcode does not show me which value in nil, and as I said the values I'm trying to pass do indeed exist.
What can I do to solve this error?
you need to declare string property for all label which is displayed to through previous view controller in profileVC.
var strName : String
In viewDidLoad
nameLabel.text = strName
In previous viewcontroller
profileVC.strName = yourstring
If use prepareForSegue then you need to pass data in this method instead of didSelect.
If you want to pass data on didSelect without prepareForSegue then you need to create instance of viewcontroller as below.
Correct following code it is only for explanation
let profileVC = self.storyboard.initiatedwithidentifier: "yourViewcontrollerIdentifier"
profileVC.strName = yourString
self.navigationviewcontroller(profileVc, animated : true)