4

我创建了一个带有Page-Based Interface的 WatchKit 应用程序。

有 3 个页面,每个页面都连接到我的InterfaceController.swift类(它扩展了 WKInterfaceController)。

我的问题:在 InterfaceController.swift 中如何检测当前视图的页码?

4

2 回答 2

2

如果你使用

func presentControllerWithNames(_ names: [AnyObject],
                   contexts contexts: [AnyObject]?)

您只需在上下文中传递页面的编号,以便您可以存储它,并在以后检索它。这是非故事板方式。

如果你使用storyboard,也是一样的,你只需要使用其他方法

func contextsForSegueWithIdentifier(_:inTable:rowIndex:)

并在每个控制器的上下文中传递您的页面索引

于 2014-12-20T13:41:09.447 回答
0

可以这样实现,

使用适当的上下文值在 pageNavigation 中显示 InterfaceController ,

[self presentControllerWithNames:@[@"TestInterfaceController",@"TestInterfaceController",@"TestInterfaceController"] contexts:@[@"Page 1",@"Page 2",@"Page 3"]];

然后创建一个 NSString 属性来跟踪页面和标签来显示它,

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

在 awakeWithContext 中将其分配给 NSString 属性,

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

    NSLog(@"%@",context);
    self.currentContext = context;
}

在 willActive 上显示它,

- (void)willActivate {
    // This method is called when watch view controller is about to be visible to user
    [super willActivate];

    NSLog(@"%@ willActivate",self.currentContext);
    [self.pageLabel setText:self.currentContext];
}

您还可以检测页面何时didDeactivate,

- (void)didDeactivate {
    // This method is called when watch view controller is no longer visible
    [super didDeactivate];
    NSLog(@"%@ didDeactivate",self.currentContext);
}

编辑: 如果页面导航是使用 Storyboard Segue 配置的,则在 Source IntefaceController 中覆盖此方法,从您创建模型 segue 的位置到目标控制器以提供上下文,

- (NSArray *)contextsForSegueWithIdentifier:(NSString *)segueIdentifier {
    NSArray *contexts = nil;
    if ([segueIdentifier isEqualToString:@"MyPageNavigation"]) {
        contexts = @[@"Page 1",@"Page 2",@"Page 3"];
    }

    return contexts;
}
于 2015-05-22T13:16:01.570 回答