Apple iWatch:上下文不在视图控制器之间发送

时间:2015-07-17 02:09:13

标签: ios objective-c iphone uitableview watchkit

所以我正在新的Apple iWatch上构建一个日历类型的应用程序。这是我的应用的初始故事板布局:

enter image description here

基本上,初始表视图将解析日历并获取事件名称及其日期。我想要做的基本上是通过push segue将数据发送到第二个视图控制器。

我尝试使用方法-(NSArray *)contextsForSegueWithIdentifier:(NSString *)segueIdentifier,但第二个视图控制器中的上下文显示为nil

这是我的代码:

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

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:2)

您不需要contextsForSegueWithIdentifier方法。 设置表后,使用此方法。

- (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]];
}

将“NibIdentifier”替换为故事板中的特定标识符。

使用此

从上下文中检索另一个控制器中的数据
- (void)awakeWithContext:(id)context 
{
    [super awakeWithContext:context];

    NSLog(@"%@",[context objectForKey:@"key1"]);
    NSLog(@"%@",[context objectForKey:@"key2"]);
    NSLog(@"%@",[context objectForKey:@"key3"]);
    NSLog(@"%@",[context objectForKey:@"key4"]);
}