Search code examples
iosobjective-ciphoneuitableviewwatchkit

Apple iWatch: contexts not sending between view controllers


So I'm building a calendar-type app on the new Apple iWatch. This is the initial storyboard layout for my app:

enter image description here

Basically the initial table view will parse the calendar and grab the event name and the date of it. What I want to do is basically, through a push segue, send that data to the second view controller.

I have tried using the method -(NSArray *)contextsForSegueWithIdentifier:(NSString *)segueIdentifier, but the context in the second view controller is showing up as nil.

This is my code:

InterfaceViewController:

#import "InterfaceController.h"
#import <EventKit/EventKit.h>
#import "Calendar.h" 



@interface InterfaceController() {
    NSArray *events;
    NSArray *eventsWithNotes;
}
@end


@implementation InterfaceController
- (void)setupTable
{


    EKEventStore *store = [[EKEventStore alloc] init];


    // Get the appropriate calendar
    NSCalendar *calendar = [NSCalendar currentCalendar];


    if ([store respondsToSelector:@selector(requestAccessToEntityType:completion:)])
    {

        [store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error)
         {
             if (granted)
             {
                 NSLog(@"User has granted permission!");
                 // Create the start date components
                 NSDateComponents *oneDayAgoComponents = [[NSDateComponents alloc] init];
                 oneDayAgoComponents.day = -1;
                 NSDate *oneDayAgo = [calendar dateByAddingComponents:oneDayAgoComponents
                                                               toDate:[NSDate date]
                                                              options:0];

                 // Create the end date components
                 NSDateComponents *oneYearFromNowComponents = [[NSDateComponents alloc] init];
                 oneYearFromNowComponents.year = 1;
                 NSDate *oneYearFromNow = [calendar dateByAddingComponents:oneYearFromNowComponents
                                                                    toDate:[NSDate date]
                                                                   options:0];

                 // Create the predicate from the event store's instance method
                 NSPredicate *predicate = [store predicateForEventsWithStartDate:oneDayAgo
                                                                         endDate:oneYearFromNow
                                                                       calendars:nil];

                 // Fetch all events that match the predicate
                 events = [store eventsMatchingPredicate:predicate];
                 NSMutableArray *rowTypesList = [NSMutableArray array];
                 for(int i=0; i < events.count; i++){
                     [rowTypesList addObject:@"Calendar"];
                 }

                 [self.tableView setRowTypes:rowTypesList];
                 for (NSInteger i = 0; i < self.tableView.numberOfRows; i++)
                 {
                     NSObject *row = [self.tableView rowControllerAtIndex:i];
                     Calendar *calendar = (Calendar *) row;

                     NSLog(@"notes: %@",[[events objectAtIndex:i] notes]);
                     NSString* notes = [[events objectAtIndex:i] notes];

                     [calendar.titleLabel setText:[[events objectAtIndex:i] title]];
                 }


             }
             else
             {
                 NSLog(@"User has not granted permission!");
             }
         }];
    }


    }

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

    // Configure interface objects here.
}


- (void)willActivate {
    // This method is called when watch view controller is about to be visible to user
    [super willActivate];
    [self setupTable];
    }

- (void)didDeactivate {
    // This method is called when watch view controller is no longer visible
    [super didDeactivate];
}

- (NSArray *)contextsForSegueWithIdentifier:(NSString *)segueIdentifier inTable:(WKInterfaceTable *)table rowIndex:(NSInteger)rowIndex {

    NSArray *array = nil;
    NSString *notes = [[events objectAtIndex:rowIndex] notes];
    NSString *title = [[events objectAtIndex:rowIndex] title];
    NSString *strippedNumber = [notes stringByReplacingOccurrencesOfString:@"[^0-9]" withString:@"" options:NSRegularExpressionSearch range:NSMakeRange(0, [notes length])];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateStyle:NSDateFormatterMediumStyle];

    NSString *date = [dateFormatter stringFromDate:[[events objectAtIndex:rowIndex] startDate]];

    if([segueIdentifier isEqualToString:@"IBM"]) {
        array = @[notes, title, strippedNumber, date];
    }
    return array;
}


@end

DetailIntefaceViewController.h:

#import <WatchKit/WatchKit.h>
#import <Foundation/Foundation.h>

@interface DetailInterfaceController : WKInterfaceController

@property (nonatomic, strong) NSString *currentContext;
@property (weak, nonatomic) IBOutlet WKInterfaceLabel *phoneNumber;

@end

DetailIntefaceViewController.m:

#import "DetailInterfaceController.h"

@interface DetailInterfaceController ()

@end

@implementation DetailInterfaceController

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

    NSLog(@"%@",context);
    self.currentContext = context;

    // Configure interface objects here.
}

- (void)willActivate {
    // This method is called when watch view controller is about to be visible to user
    [super willActivate];
    NSLog(@"%@ willActivate",self.currentContext);
    [self.phoneNumber setText:self.currentContext];
}

- (void)didDeactivate {
    // This method is called when watch view controller is no longer visible
    [super didDeactivate];
    NSLog(@"%@ didDeactivate",self.currentContext);
}

@end

Any help would be appreciated.


Solution

  • You don't need that contextsForSegueWithIdentifier method. After setting up the table, use this method.

    - (void)table:(WKInterfaceTable *)table didSelectRowAtIndex:(NSInteger)rowIndex
    {
        NSString *notes = [[events objectAtIndex:rowIndex] notes];
        NSString *title = [[events objectAtIndex:rowIndex] title];
        NSString *strippedNumber = [notes stringByReplacingOccurrencesOfString:@"[^0-9]" withString:@"" options:NSRegularExpressionSearch range:NSMakeRange(0, [notes length])];
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
    
        NSString *date = [dateFormatter stringFromDate:[[events objectAtIndex:rowIndex] startDate]];
    
     //You can push controller instead of segue like this and sending the variable data as a dictionary in context,
    
        [self pushControllerWithName:@"NibIdentifier" context:[NSDictionary dictionaryWithObjectsAndKeys:notes,@"notes",title,@"title",strippedNumber,@"strippedNumber",date,@"date",nil]];
    }
    

    Replace "NibIdentifier" with your specific identifier from storyboard.

    Retrieve the data in another controller from context using this,

    - (void)awakeWithContext:(id)context 
    {
        [super awakeWithContext:context];
    
        NSLog(@"%@",[context objectForKey:@"key1"]);
        NSLog(@"%@",[context objectForKey:@"key2"]);
        NSLog(@"%@",[context objectForKey:@"key3"]);
        NSLog(@"%@",[context objectForKey:@"key4"]);
    }