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

Ios Apple iWatch:视图控制器之间不发送上下文,ios,objective-c,iphone,uitableview,watchkit,Ios,Objective C,Iphone,Uitableview,Watchkit,因此,我正在新的苹果iWatch上构建一个日历类型的应用程序。这是我的应用程序的初始情节提要布局: 基本上,初始表视图将解析日历并获取事件名称和日期。我想做的基本上是,通过推送序列,将数据发送到第二个视图控制器 我已尝试使用方法-(NSArray*)contextsForSegueWithIdentifier:(NSString*)segueinidentifier,但第二个视图控制器中的上下文显示为nil 这是我的代码: 接口设备控制器: #import "InterfaceControll

因此,我正在新的苹果iWatch上构建一个日历类型的应用程序。这是我的应用程序的初始情节提要布局:

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

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

这是我的代码:

接口设备控制器:

#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
#import <WatchKit/WatchKit.h>
#import <Foundation/Foundation.h>

@interface DetailInterfaceController : WKInterfaceController

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

@end
#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

任何帮助都将不胜感激。

您不需要使用
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"]);
}

在if处添加断点([segueIdentifier isEqualToString:@“IBM”])。检查它是否在这种情况下运行。我是否理解您的意思,当单击表行时,您希望推送到新控制器?@iOSNoob我创建了一个断点,它确实运行code@pi1000是的,这正是我想要的,因为苹果文件上说,数组中的对象数必须与作为segue目标的基于页面的接口中存在的接口控制器数匹配。我有一个问题。我将在何处调用此方法
[self-pushControllerWithName:@“NibIdentifier”上下文:[NSDictionary Dictionary WithObjectsAndKeys:object1、@“key1”、object2、@“key2”、object3、@“key3”、ObjectJ‌​ect4,@“key4”,nil]]?我需要舞台表演。我没有检查segue标识符,所以它会自动推送到新的视图控制器。非常感谢。这正是我需要的Hi@VishwaIyer,可以分享你的skype id。我想讨论一下apple watck OS 2