GCMNavStackMachine 0.1.3

GCMNavStackMachine 0.1.3

测试已测试
Lang语言 Obj-CObjective C
许可证 自定义
发布最新发布2016 年 5 月

Jerry HsuJerry HsuPhil Sarinbernberg11 维护。



  • 作者
  • Phil Sarin

Build status

GCMNavStackMachine 是一个由 UINavigationController 支持的状态机。API 吁托于 YBStatechart,但我们特别针对 UINavigationControllers 进行了调整。

问题

UINavigationController 将显示一系列 UIViewControllers,但这些 UIViewControllers 不应过多了解他们的上下文。也就是说,它们不应该知道在它们之前或之后显示了哪些视图控制器。这种知识限制了重用性。

一个很好的模式是将流程逻辑拉入状态机中,该状态机负责处理事件并在子 UIViewControllers 之间切换 UINavigationController。子视图控制器对状态机一无所知,并且可以轻松地在其他状态机中重新使用。

示例

_stackMachine = [[GCMNavStackMachine alloc] initWithNavigationController:navigationController];
MYViewController *vc1 = [[MYViewController alloc] init];
MYViewController *vc2 = [[MYViewController alloc] init];
vc1.delegate = self;
vc2.delegate = self;

[navigationController pushViewController:vc1 animated:YES];
[_stackMachine.onEvent:@"advance" forController:vc1 doBlock:^{
  [navigationController pushViewController:vc2 animated:YES];
}];
[_stackMachine.onEvent:@"advance" forController:vc2 doBlock:^(id payload){
  [weakSelf save];
  [weakSelf.presentingViewController dismissViewControllerAnimated:YES completion:nil];
}];
[_stackMachine.onEvent:@"cancel" doBlock:^(id payload){
  [weakSelf cancel];
  [weakSelf.presentingViewController dismissViewControllerAnimated:YES completion:nil];
}];
...

#pragma mark - MYViewControllerDelegate

- (void)myViewControllerDidAdvance:(MYViewController *)myVC {
  // if we receive this message for vc1, then we should advance to vc2
  // if we receive this message for vc2, we should save and commit
  // the stack machine will handle those details
  [_stackMachine dispatchEvent:@"advance"];
}

- (void)myViewControllerDidCancel:(MYViewController *)myVC {
  [_stackMachine dispatchEvent:@"cancel"];
}