Search code examples
ioswatchkit

Watchkit - passing data to source controller


I've currently setup my watch kit to pass data from source to destination using the following:

Source

- (IBAction)changeRep {
NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:@"rep", @"button", nil];
[self presentControllerWithName:@"KeyPadInterfaceController" context:dictionary];

}

Destination

- (void)awakeWithContext:(id)context {
[super awakeWithContext:context];


_parent = [context valueForKey:@"button"];



}

I'm trying to get the data now from the destination to source view using the following but dataFromKeyPad in the source view isn't being called.

Source.h

@interface WorkoutDetailInterfaceController : WKInterfaceController <KeyPadInterfaceControllerDelegate>{

Source.m

- (void)dataFromKeyPad:(NSDictionary *)data {
if ([data objectForKey:@"rep"]){
    _repNum = [data valueForKey:@"rep"];
    NSString *repTitle = [NSString stringWithFormat:NSLocalizedString(@"%@ reps", "Number of Reps"), _repNum];
    [self.reps setTitle:repTitle];
} else if ([data objectForKey:@"weight"]) {
    _weightNum = [data valueForKey:@"weight"];
    NSString *weightTitle = [NSString stringWithFormat:NSLocalizedString(@"%@ reps", "Number of Reps"), _weightNum];
    [self.reps setTitle:weightTitle];
}
}

Destination.h

@protocol KeyPadInterfaceControllerDelegate <NSObject>

- (void)dataFromKeyPad:(NSDictionary *)data;

@end

@property (nonatomic, weak) id<KeyPadInterfaceControllerDelegate> delegate;

Destination.m

- (IBAction)okAct{
NSDictionary *dictionary;

if ([_parent isEqualToString:@"rep"]) {
    dictionary  = [[NSDictionary alloc] initWithObjectsAndKeys:_result, @"rep", nil];
} else {
    dictionary =[[NSDictionary alloc] initWithObjectsAndKeys:_result, @"weight", nil];
}
[self.delegate dataFromKeyPad:dictionary];

[self dismissController];
}

When I press the ok button, okAct is called and it goes through everything including dismissController but [self.delegate dataFromKeyPad:dictionary]; doesn't trigger anything in the source view. Any suggestions? I need a solution in Objective C.


Solution

  • What value is set in self.delegate of Destination.m? It might contain nil in self.delegate.

    And You should do as follows in order to prevent crash.

    if ([self.delegate respondsToSelector:@selector(dataFromKeyPad:)])
    {
        [self.delegate dataFromKeyPad:dictionary];
    }
    

    Added (7/29): The way to set self.delegate

    Source

    - (IBAction)changeRep {
        NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:
                                    @"rep", @"button",
                                    self, @"delegate",
                                    nil];
        [self presentControllerWithName:@"KeyPadInterfaceController" context:dictionary];
    }
    

    Destination

    - (void)awakeWithContext:(id)context
    {
        [super awakeWithContext:context];
    
        // Configure interface objects here.
        if ([context isKindOfClass:[NSDictionary class]]) {
            self.delegate = [context objectForKey:@"delegate"];
        } 
    }