我使用-[WKInterfaceController presentControllerWithNames:contexts:]
以模态方式呈现动态创建的一组页面。在某些情况下,我希望在页面可见时插入页面。这是可能的,还是一旦可见就修复了一组页面?
答案 0 :(得分:0)
我设计了一种实现这一目标的方法,但它很麻烦。更好的解决方案是相当大的改进。
WKInterfaceController
都有UUID
属性,该属性使用其上下文分配并存储在awakeWithContext:
期间WKInterfaceController
在显示之前保留WKInterfaceController
个名称和上下文的数组WKInterfaceController
WKInterfaceController
在其保留的WKInterfaceController
名称和上下文数组中进行所需的更改WKInterfaceController
取消当前的页面组,然后使用更新的名称/上下文数组重新呈现它们WKInterfaceController
发布所有页面都会观察到的通知,包括新页面的UUID
作为通知对象。通知会触发页面检查自己的UUID
与通知对象中发送的页面。如果通知匹配,则该接口控制器调用becomeCurrentPage
我还没有对此进行过测试,但这是我能想到的唯一可行的方法。它会很难看,因为你会看到当前页面消失,然后第一页出现,然后新页面动画进入视图。从用户体验的角度来看并不是很好,但我无法想到任何其他方式,因为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];
}