gpt4 book ai didi

ios - 苹果 watch : contexts not sending between view controllers

转载 作者:行者123 更新时间:2023-11-28 21:42:35 24 4
gpt4 key购买 nike

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

enter image description here

基本上,初始 TableView 将解析日历并获取事件名称和日期。我想做的基本上是通过推送转场将该数据发送到第二个 View Controller 。

我已经尝试使用方法 -(NSArray *)contextsForSegueWithIdentifier:(NSString *)segueIdentifier,但是第二个 View Controller 中的上下文显示为 nil

这是我的代码:

接口(interface) View Controller :

#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

如有任何帮助,我们将不胜感激。

最佳答案

您不需要那个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”替换为 Storyboard中的特定标识符。

使用它从上下文中检索另一个 Controller 中的数据,

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

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

关于ios - 苹果 watch : contexts not sending between view controllers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31467395/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com