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

时间:2016-04-19 08:13:40

标签: navigation watch-os-2 watch-os

我使用-[WKInterfaceController presentControllerWithNames:contexts:]以模态方式呈现动态创建的一组页面。在某些情况下,我希望在页面可见时插入页面。这是可能的,还是一旦可见就修复了一组页面?

2 个答案:

答案 0 :(得分:0)

我设计了一种实现这一目标的方法,但它很麻烦。更好的解决方案是相当大的改进。

  1. 页面组中的每个WKInterfaceController都有UUID属性,该属性使用其上下文分配并存储在awakeWithContext:期间
  2. 显示页面组的WKInterfaceController在显示之前保留WKInterfaceController个名称和上下文的数组
  3. 当其中一个页面需要插入/删除该页面中的页面时,它会将通知发回给呈现WKInterfaceController
  4. 呈现WKInterfaceController在其保留的WKInterfaceController名称和上下文数组中进行所需的更改
  5. 呈现WKInterfaceController取消当前的页面组,然后使用更新的名称/上下文数组重新呈现它们
  6. 呈现WKInterfaceController发布所有页面都会观察到的通知,包括新页面的UUID作为通知对象。通知会触发页面检查自己的UUID与通知对象中发送的页面。如果通知匹配,则该接口控制器调用becomeCurrentPage
  7. 我还没有对此进行过测试,但这是我能想到的唯一可行的方法。它会很难看,因为你会看到当前页面消失,然后第一页出现,然后新页面动画进入视图。从用户体验的角度来看并不是很好,但我无法想到任何其他方式,因为Apple没有给我们任何插入页面的方法。

答案 1 :(得分:0)

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

/*
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];
}