swiftseguewatchkitwkinterfacecontroller

context showing nil when trying to send data across Interface Controllers in WatchKit


I have two Interface Controllers with FirstInterfaceController being the one where I have a context object I want to send to SecondInterfaceController. I am wondering why context is showing up as nil when I expect it to be 0 when I tap the buttonAction.

Metadata is defined as follows:

class Metadata {
    var x = 0

    init (x_parameter: Int) {
        x = x_parameter
    }    
}

Here is my definition of FirstInterfaceController:

class FirstInterfaceController: WKInterfaceController {

    var metadata = Metadata(x: 1)

    @IBAction func buttonAction() {
        metadata.x = 0

        // Use pushControllerWithName for a push segue
        pushController(withName: "Ten Point Tiebreaker", context: metadata)
    }
}

Here is my Identifier for the SecondInterfaceController enter image description here

Here is the definition of SecondInterfaceController:

class SecondInterfaceController: WKInterfaceController {

    override func awake(withContext context: Any?) {
        super.awake(withContext: context)
        print("context: \(String(describing: context))")    // This prints nil

        // Configure interface objects here.
        if let metadata = context as? Metadata {
            print("x: \(metadata.x)")
        }
    }
}

Solution

  • The problem I had was that I also had a Push segue so that was causing some issues. I just removed the Push segue and it worked fine.