14

I have an interface with 2 buttons, both of them call the same interface but with different information. On the traditional interface I use prepareForSegue, but I don't know what's the equivalent one on the WatchKit.

Nikos M.
  • 13,685
  • 4
  • 47
  • 61

4 Answers4

35

You can do this in two ways:

In your storyboard you set an identifier in your segue:

enter image description here

and then you can use contextForSegueWithIdentifier:

- (id)contextForSegueWithIdentifier:(NSString *)segueIdentifier {
     if ([segueIdentifier isEqualToString:@"yourIdentifier"]) {
        return aDictionaryWithYourInformation;
     }
}

Or you can pass information with a context via code, with:

[self pushControllerWithName:@"YourViewController"
                     context:aDictionary];

This context is a dictionary and you have access to this dictionary in the - (void)awakeWithContext:(id)context

Tiago Almeida
  • 14,081
  • 3
  • 67
  • 82
  • Correct answer, similar Q&A: https://stackoverflow.com/questions/27021186/passing-data-back-from-a-modal-view-in-watchkit/46421098#46421098 – BootMaker Sep 26 '17 at 13:15
11

For segue navigation in Watchkit there are two methods in WKInterfaceController:

override func contextForSegueWithIdentifier(segueIdentifier: String) -> AnyObject? {
        return //your object
    }

and for tables

override func contextsForSegueWithIdentifier(segueIdentifier: String, inTable table: WKInterfaceTable, rowIndex: Int) -> [AnyObject]? {
      return  //your object
    }

you can get the object you are passing in func awakeWithContext(context: AnyObject?) of the interface controller you are pushing

Nikos M.
  • 13,685
  • 4
  • 47
  • 61
2

In WatchKit, you can use this for calling WKInterfaceController:

[self pushControllerWithName:@"YourControlName"
          context:[self contextForSegueWithIdentifier:@"YourControlName"]];
Pang
  • 9,564
  • 146
  • 81
  • 122
1

For tables it's the following:

override func contextForSegue(withIdentifier segueIdentifier: String, in table: WKInterfaceTable, rowIndex: Int) -> Any? {
     return  //your object
    }
Sebbo
  • 326
  • 4
  • 10