react-native-navigation的迁移库

RNNControllerFactory.m 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #import "RNNControllerFactory.h"
  2. #import "RNNLayoutNode.h"
  3. #import "RNNRootViewController.h"
  4. @interface RNNControllerFactory ()
  5. @property (nonatomic, strong) id<RNNRootViewCreator> creator;
  6. @end
  7. @implementation RNNControllerFactory
  8. # pragma mark public
  9. -(instancetype)initWithRootViewCreator:(id <RNNRootViewCreator>)creator {
  10. self = [super init];
  11. self.creator = creator;
  12. return self;
  13. }
  14. -(UIViewController *)createLayout:(NSDictionary *)layout
  15. {
  16. return [self fromTree:layout];
  17. }
  18. # pragma mark private
  19. -(UIViewController*)fromTree:(NSDictionary*)json
  20. {
  21. RNNLayoutNode* node = [RNNLayoutNode create:json];
  22. if (node.isContainer)
  23. {
  24. return [self createContainer:node];
  25. } else if (node.isContainerStack)
  26. {
  27. return [self createContainerStack:node];
  28. } else if (node.isTabs)
  29. {
  30. return [self createTabs:node];
  31. }
  32. @throw [NSException exceptionWithName:@"UnknownControllerType" reason:[@"Unknown controller type " stringByAppendingString:node.type] userInfo:nil];
  33. }
  34. -(UIViewController*)createContainer:(RNNLayoutNode*)node
  35. {
  36. return [[RNNRootViewController alloc] initWithNode:node rootViewCreator:self.creator];
  37. }
  38. -(UINavigationController*)createContainerStack:(RNNLayoutNode*)node
  39. {
  40. UINavigationController* vc = [[UINavigationController alloc] init];
  41. NSMutableArray* controllers = [NSMutableArray new];
  42. for (NSDictionary* child in node.children) {
  43. [controllers addObject:[self fromTree:child]];
  44. }
  45. [vc setViewControllers:controllers];
  46. return vc;
  47. }
  48. -(UITabBarController*)createTabs:(RNNLayoutNode*)node
  49. {
  50. UITabBarController* vc = [[UITabBarController alloc] init];
  51. NSMutableArray* controllers = [NSMutableArray new];
  52. for (NSDictionary* child in node.children) {
  53. UIViewController* childVc = [self fromTree:child];
  54. UITabBarItem* item = [[UITabBarItem alloc] initWithTitle:@"A Tab" image:nil tag:1];
  55. [childVc setTabBarItem:item];
  56. [controllers addObject:childVc];
  57. }
  58. [vc setViewControllers:controllers];
  59. return vc;
  60. }
  61. @end