1

I've been struggling over the past little while with something that should be dead easy, but I just can't seem to get to work. I'm bolting on a WatchKit interface to an existing iPhone app that I have for sale in the App Store, developed in Objective-C. I simply want to pass an object to the next WKInterfaceController when I swipe to segue to it.

Given WatchKit doesn't have prepareForSegue, and page-based Relationship Segues don't have identifiers (from what I can tell) which would enable me to use contextForSegueWithIdentifier, how should I go about passing an object?

I have tried:

 _names = [[NSArray alloc]initWithObjects:@"RootInterfaceController",@"StatusInterfaceController",nil];
NSString *passedBAL=@"TEST";
    _contextObject=[[NSArray alloc]initWithObjects:passedBAL, nil];
    if (firstLoad) {
        [WKInterfaceController reloadRootControllersWithNames:_names
                                                     contexts:_contextObject];
        firstLoad=NO;

in -(void)willActivate, where I have set a value for the _passedBal property in -(void)awakeWithContext:(id)context, and a BOOL to avoid the infinite recursion this method seems to create (very kludgy fix in my view), yet the value is always nil when I arrive at the next WKInterfaceController. When I set breakpoints and mouse over the variable in the StatusInterfaceController, it seems to set the value, but as the program continues to execute, it nils it out immediately, though not in response to anything I have written in my code.

Any help would be appreciated, as I have been pulling out my hair on this one, and as I said, it should dead simple.

EDIT:

_passedBAL was a simple NSString property, but i've changed it in the above to just a straight-up NSString to put in the array for clarity's sake and added my StatusInterfaceController code. Thanks in advance :)

In my StatusInterfaceController.h , I have the following:

#import <WatchKit/WatchKit.h>
#import <Foundation/Foundation.h>
#import "InterfaceController.h"
@interface StatusInterfaceController : WKInterfaceController
@property (strong, nonatomic) NSString *passedBAL;

@end

While in my StatusInterfaceController.m I have:

#import "StatusInterfaceController.h"

@interface StatusInterfaceController ()
@end

@implementation StatusInterfaceController

- (void)awakeWithContext:context {
    [super awakeWithContext:context];
    _passedBAL=[context objectAtIndex:0];
    NSLog(@"Passed BAL is equal to %@",_passedBAL);
    // Configure interface objects here.
}
Hima
  • 1,249
  • 1
  • 14
  • 18
Ray Richards
  • 117
  • 2
  • 11

1 Answers1

4

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

EDIT

Now that we can see more code it seems to me that there is some confusion in your reload and awakeWithContext.

In the reload method you pass an array of contexts, that basically is a context for each interface. Then, you receive a single context in your awakeWithContext, that in your case is the string you want. Therefore, your awakeWithContext in your Root Interface Controller should be the context:

- (void)awakeWithContext:context {
    [super awakeWithContext:context];
   NSLog(@"Passed BAL is equal to %@",context);
}

This method will be nil in your StatusInterfaceController since you are not passing any context to it.

Tiago Almeida
  • 14,081
  • 3
  • 67
  • 82
  • Thanks, but from what I can tell, you can't set an identifier for a Relationship Segue (page-based navigation). When you highlight it, there are no options available under the attributes menu. In your second solution, I would need to create a button to segue, when I just want the info passed on a swipe (which is also unavailable to add in WatchKit as an interface object apparently). Any ideas on how I would do that? – Ray Richards Apr 20 '15 at 17:48
  • Sorry, didn't understand you were talking about a page based navigation (please make it clear in your question to narrow future answers). As far as I know the best method is to reload your view controllers with different contexts, and it seems you are already doing that. – Tiago Almeida Apr 20 '15 at 17:56
  • Thanks Tiago, perhaps my code is screwy at the receiving end then... In my receiving WKViewController, I have - (void)awakeWithContext:context { [super awakeWithContext:context]; _passedBAL=[context objectAtIndex:0]; NSLog(@"Passed BAL is equal to %@",_passedBAL); Where I have a property set up and given I am passing that value in an NSArray, I have selected the first object. Is that incorrect? – Ray Richards Apr 20 '15 at 18:05
  • @RayRichards please post your _passedBAL that you are passing in the initWithObjects and update with the receiving code so I can help. It seems that the problem is there. – Tiago Almeida Apr 20 '15 at 21:33
  • Thanks Tiago, I edited the original to comply with your request. I've tried tons of different permutations of the receiver class, but with no success, so I posted the simplest in hopes you could tell me where I am going wrong :) – Ray Richards Apr 20 '15 at 23:10
  • @RayRichards your awake withContext doesn't receive an array, it receives your string. when you pass an array in the reloadRootControllers you are passing N contexts for N controllers. – Tiago Almeida Apr 20 '15 at 23:42
  • Thanks Tiago, however if I change it to _passedBAL=context; seeing as they are both supposed to be strings, it is still nil. – Ray Richards Apr 20 '15 at 23:44
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/75762/discussion-between-tiago-almeida-and-ray-richards). – Tiago Almeida Apr 20 '15 at 23:44