Dread
Dread

Reputation: 87

Crashing when passing Var data as delegates

I have been trying to pass data back to my "HostRideViewController" from my "ProfileViewController" but the data I try to pass back creates a fatal error when unwrapping it, but i dont understand why as the console print out before it shows the correct data

import UIKit
import Foundation
import Firebase


protocol RiderSelectionDelegate {
    func selectedRideLeader(firstName:String,lastName:String,uid:String)
}
class ProfileViewController: UIViewController {
    @IBOutlet weak var ProfilePictureImageView: UIImageView!
    @IBOutlet weak var AddasLeaderBtn: UIButton!
    @IBOutlet weak var FirstNameFeild: UILabel!
    @IBOutlet weak var LastNameFeild: UILabel!
    @IBOutlet weak var UserBioFeild: UILabel!
    @IBOutlet weak var HiddenEmail: UILabel!
    @IBOutlet weak var HiddenUID: UILabel!
    
    var user: MyUser?
    var selectedFirstName: String?
    var selectedLastName: String?
    var selectedUID: String?
    var selectedEmail: String?
    var selectionDelegate: RiderSelectionDelegate!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        /// make profile picture circular
        ProfilePictureImageView.layer.cornerRadius = ProfilePictureImageView.frame.size.width/2
        ProfilePictureImageView.clipsToBounds = true
       /// load user data into view
        FirstNameFeild?.text = user?.firstName
        LastNameFeild?.text = user?.lastName
        HiddenUID?.text = user?.uid
        HiddenEmail?.text = user?.email
    }
    @IBAction func SelectedLeaderpressed(_ sender: Any) {
        selectedFirstName = FirstNameFeild.text
        selectedLastName = LastNameFeild.text
        selectedUID = user?.uid
        selectedEmail = user?.email
        
        print(selectedUID!,selectedLastName!,selectedFirstName!)
        
        /// where the error is happening
        selectionDelegate.selectedRideLeader(firstName:selectedFirstName!, lastName:selectedLastName!, uid:selectedUID!)
        dismiss(animated: true, completion: nil)
    }
}

My console output and error from passing data

49s64wH2g7hjiMAKp0uvADkzP0k2 bloggs joe

Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value: file

My intended use of the delegate is to set a label up with the user's name

  extension HostRideViewController:RiderSelectionDelegate{
    func selectedRideLeader(firstName: String, lastName: String, uid: String) {
        OtherRideLeaderLbl.text = firstName
    }
}

where am I going wrong as I'm trying to force unwrap the data which is there and present in the print but crashes when I'm trying to delegate it

Upvotes: 0

Views: 42

Answers (1)

Shehata Gamal
Shehata Gamal

Reputation: 100503

It seems selectionDelegate is nil you need to set it when you present/push/segue to the destination vc

Upvotes: 2

Related Questions