以编程方式在事件中导航到基于页面的界面中的后页

时间:2015-05-13 16:37:21

标签: objective-c interface navigation watchkit

我的应用程序是基于页面的,我希望在一个事件上我的应用程序以编程方式滑动到左页。我尝试使用becomeCurrentPage方法,但在这种情况下如何使用它。 请帮忙。 任何建议将不胜感激。 感谢

1 个答案:

答案 0 :(得分:0)

becomeCurrentPage是以编程方式更改当前页面的唯一方法,因此您可以走上正确的轨道。但是,听起来你需要从一个页面到另一个页面进行通信。

假设您的接口控制器知道自己的页面索引(可以传递给awakeWithContext:),您可以通过发送自定义通知来完成此操作:

NSDictionary *userInfo = @{ @"page" : @(MAX(self.pageIndex - 1, 0)) };
[[NSNotificationCenter defaultCenter] postNotificationName:@"MyChangePageNotification"
                                                        object:nil
                                                      userInfo:userInfo];

然后,在每个控制器中,确保您正在收听通知:

[[NSNotificationCenter defaultCenter]
 addObserverForName:@"MyChangePageNotification"
 object:nil
 queue:nil
 usingBlock:^(NSNotification *note) {

     // Are we supposed to become the current page?
     if (self.pageIndex == [note.userInfo[@"page"] integerValue]) {
         [self becomeCurrentPage];
     }
 }];

基本上,每个页面都会侦听您的自定义通知,当传递的页面索引与控制器的索引匹配时,它将成为当前页面。

相关问题