WatchOS:在基于页面的导航中插入页面



我正在使用-[WKInterfaceController presentControllerWithNames:contexts:]来模式地呈现一组动态创建的页面。在某些情况下,我想在页面集中插入一个页面,而它们可见。这是可能的,还是一组页面一旦可见就固定了?

我已经设计了一种实现此目的的方法,但它很混乱。更好的解决方案将是相当大的改进。

  1. 页面组中的每个WKInterfaceController都有一个 UUID 属性,该属性使用其上下文进行分配,并在awakeWithContext:期间存储
  2. 显示页面组的WKInterfaceController在呈现页面之前保留WKInterfaceController名称和上下文的数组
  3. 当其中一个页面需要从集合中插入/删除页面时,它会将通知发布回演示WKInterfaceController
  4. 呈现WKInterfaceController在其保留的WKInterfaceController名称和上下文数组中进行所需的更改
  5. 呈现WKInterfaceController关闭当前页面组,然后使用更新的名称/上下文数组重新显示它们
  6. 呈现WKInterfaceController发布一个通知,该通知由所有页面观察,包括作为通知对象的新页面的UUID。通知会触发页面根据通知对象中发送的页面检查自己的UUID。如果通知匹配,则该接口控制器将调用becomeCurrentPage

我还没有实际测试过这个,但这是我能想到的唯一可能有效的方法。不过,这会很丑陋,因为您会看到当前页面消失,然后出现第一页,然后新页面以动画形式显示在视图中。从用户体验的角度来看不是很好,但我想不出任何其他方法,因为 Apple 没有给我们任何插入页面的方法。

这是我为此设计的解决方案。

/*
MyInterfaceController - class within the set of pages
*/
- (void)awakeWithContext:(id)context {
    [super awakeWithContext:context];
    NSDictionary *dict = (NSDictionary*)context;
    self.uuid = [dict valueForKey:@"uuid"];
    self.jumpuuid = [dict valueForKey:@"jumpuuid"];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkBecomeCurrentPage:) name:@"checkBecomeCurrentPage" object:nil];
}
- (void)checkBecomeCurrentPage:(NSNotification*)notification {
    NSString *checkUUID = (NSString*)notification.object;
    if ([self.uuid isEqualToString:checkUUID]) {
        [self becomeCurrentPage];
    }
}
- (void)didAppear {
    [super didAppear];
    if (self.jumpuuid) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"checkBecomeCurrentPage" object:self.jumpuuid];
    }
}
- (IBAction)addPageButtonAction {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"addPageNotification" object:self];
}
#warning Populate this with the names and contexts for the set of pages when initially displaying them. It is used for inserting pages later
+ (NSMutableDictionary*)namesAndContextsForCurrentlyDisplayedPage {
    static NSMutableDictionary *dict = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        dict = [NSMutableDictionary dictionary];
    });
    return dict;
}
+ (void)addPageWithPresentingController:(WKInterfaceController *)presentingController requestingInterfaceController:(MyInterfaceController *)requestingInterfaceController {
    NSMutableArray *contexts = [[[MyInterfaceController namesAndContextsForCurrentlyDisplayedPages] objectForKey:@"contexts"] mutableCopy];
    NSInteger requestingInterfaceControllerIndex = -1;
    for (NSDictionary *dict in contexts) {
        if ([[dict valueForKey:@"uuid"] isEqualToString:requestingInterfaceController.uuid]) {
            requestingInterfaceControllerIndex = [contexts indexOfObject:dict];
        }
    }
    if (requestingSetInterfaceControllerIndex == -1) {
        #warning Display an error that the position of the current page could not be established
    }
    else {
        NSInteger insertIndex = requestingInterfaceControllerIndex + 1;
        NSMutableArray *names = [[[MyInterfaceController namesAndContextsForCurrentlyDisplayedPages] objectForKey:@"names"] mutableCopy];
        [names insertObject:@"MyInterfaceController" atIndex:insertIndex];
        NSString *newUUID = [[NSUUID UUID] UUIDString];
        NSDictionary *newDict = @{@"uuid": newUUID};
        [contexts insertObject:newdict atIndex:insertIndex];
        NSMutableDictionary *firstDict = [contexts.firstObject mutableCopy];
        [firstDict setValue:newUUID forKey:@"jumpuuid"];
        [contexts replaceObjectAtIndex:0 withObject:firstDict];
        [[MyInterfaceController namesAndContextsForCurrentlyDisplayedPages] setValue:names forKey:@"names"];
        [[MyInterfaceController namesAndContextsForCurrentlyDisplayedPages] setValue:contexts forKey:@"contexts"];
        [presentingController dismissController];
        [presentingController presentControllerWithNames:names contexts:contexts];
    }
}

-

/*
MyParentInterfaceController - the controller which originally presented the set of pages
*/
- (void)awakeWithContext:(id)context {
    [super awakeWithContext:context];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addPage:) name:@"addPageNotification" object:nil];
}
- (void)addPage:(NSNotification*)notification {
    [MyInterfaceController addPageWithPresentingController:self requestingInterfaceController:notification.object];
}

相关内容

  • 没有找到相关文章

最新更新