ctdewaters
ctdewaters

Reputation: 146

Unable to Pass Data Between Interface Controllers

I'm having a problem passing a string to a new interface controller in my WatchKit app. The segue to the new interface controller is hooked up in the storyboard from the table row. Here is my code for the selection of a table row:

  var selectedElement: String!
  override func table(table: WKInterfaceTable, didSelectRowAtIndex rowIndex: Int) {
    switch rowIndex{
    case 0:
        selectedElement = menuElements.objectAtIndex(0) as! String
    case 1:
        selectedElement = menuElements.objectAtIndex(1) as! String
    case 2:
        selectedElement = menuElements.objectAtIndex(2) as! String
    default:
        break
    }
}

Then I am using this method to pass the selectedElement variable as the context:

  override func contextForSegueWithIdentifier(segueIdentifier: String) -> AnyObject? {
    return selectedElement
 }

In my SecondInterfaceController class, I am trying to use this code to display the context string in a label:

override func awakeWithContext(context: AnyObject?) {
    super.awakeWithContext(context)
    label.setText(context as? String)
 }

This produces a blank label, and I also tried printing the context to the console, and it returned nil.

Thanks in advance for your help.

Upvotes: 1

Views: 474

Answers (1)

Sohel L.
Sohel L.

Reputation: 9540

To pass data between interface controllers you need add this method

pushControllerWithName:context:

Upvotes: 2

Related Questions