Bob
Bob

Reputation: 8724

Passing object between view controllers in Swift

I am having a strange issue. I am trying to pass an object from ViewControllerA to CreateInvitationViewController.

In ViewControllerA I have following code:

func btnPassedRequestTouched(sender:UIButton!)
{
    print("button passed request touched")
    // pass request
    self.performSegueWithIdentifier("createInvitationSegue", sender: self)

}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
    if segue.identifier == "createInvitationSegue" {
        let createInvitationView = segue.destinationViewController as! CreateInvitationViewController
        let invitation = invitations[self.carousel.currentItemIndex]
        createInvitationView.invitation = invitation
    }
}

I put a breakpoint on this line: createInvitationView.invitation = invitation, and I can see that this object exists.

In ViewControllerB I have the following code:

class CreateInvitationViewController: UIViewController {

    var invitation = Invitation()

    override func viewDidLoad() {
        super.viewDidLoad()

        // if invitation is set, then use it to populate fields
        if let _ = self.invitation.id {
            invitationText.hidden = false
            invitationText.text = self.invitation.note
        }
    }

I am using Show action on the segue that I made and it looks like this: Show action This is the error that I am getting:

Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<MyProject.CreateInvitationViewController 0x156365810> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key postRequest.'

Do you have some suggestion why this happens?

Upvotes: 1

Views: 47

Answers (1)

Rob
Rob

Reputation: 438297

It's telling you it cannot find postRequest. That means that you either have some code that is trying to set postRequest or, more likely, you have some control in your storyboard that is trying to connect to an outlet called postRequest, but no such outlet exists (e.g. maybe you had some outlet with that name in the past, removed the outlet from the code, but neglected to update the storyboard accordingly).

Upvotes: 3

Related Questions