1

I have a class of User that has a lot of user data in it. Like a contact app, I would like when someone taps on the contact it loads a contact card. Usually I use prepareForSegue and a storyboard segue to pass values of String, Int, Array, etc but I can't seem to figure out how to send a value of type User. On the sending side, I've set it up correctly but I can't figure out how to set up the receiving view. The reason why I want to send the whole thing instead of using dot syntax and grabbing each element individually is because (like the contacts app) I want the user to be able to add fields to the contact card and I don't want to have to type out all of the data that I need to pass unless I have to.

User class:

class User {
    var firstName = ""
    var lastName = ""
    var email = ""
    var birthday = ""
    var team = ""
    var schedule = ""
    var service = ""
    var position = ""
    var phoneNumber = ""
    var dateStarted = ""
    var profileImgURL = ""
    var notes = ""

    init (firstName:String, lastName:String, email:String, birthday:String, team:String, schedule:String, service:String, position:String, phoneNumber:String, dateStarted:String, profileImgURL:String, notes:String) {
        self.firstName = firstName
        self.lastName = lastName
        self.email = email
        self.birthday = birthday
        self.team = team
        self.schedule = schedule
        self.service = service
        self.position = position
        self.phoneNumber = phoneNumber
        self.dateStarted = dateStarted
        self.profileImgURL = profileImgURL
        self.notes = notes
    }

Sender:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "SeeContactCardSegue" {
        if let destVC = segue.destinationViewController as? ContactCardTVC {
            if let indexPath = tableView.indexPathForSelectedRow {
                let user = users[indexPath.row]
                destVC.user = user
            }
        }
    }
}

Receiver:

var user:User = ???

I get an error saying user needs to be initialized, which I get in most situations but I am not sure how to initialize this. Am I missing something?

Michael Williams
  • 1,402
  • 2
  • 14
  • 27

2 Answers2

0

I have a feeling you ran into the same issue I did if you're showing the different view via a modal segue--that the destinationViewController is a UINavigationController not your controller. 1st verify in the debugger that you're actually going into the if let destVC = segue.destinationViewController as? ContactCardTVC block and that the segue.destinationViewController is indeed ContactCardTVC

If my assumption is correct that your destinationViewController is a UINavigationController your code should look something like the following:

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)     {
    if segue.identifier == "SeeContactCardSegue" {
        let nav = segue.destinationViewController as! UINavigationController
        let cardContactVC = nav.topViewController as! ContactCardTVC   
            if let indexPath = tableView.indexPathForSelectedRow {
                let user = users[indexPath.row]
                cardContactVC.user = user
            }

    }
}
DannyP
  • 200
  • 2
  • 10
0

Force unwrap it. It should work fine.

var user: User!

Abhishek729
  • 327
  • 5
  • 14