测试已测试 | ✓ |
Lang语言 | Obj-CObjective C |
许可证 | 自定义 |
发布最新发布 | 2016 年 5 月 |
由 Jerry Hsu、Jerry Hsu、Phil Sarin、bernberg11 维护。
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"];
}