react-native-navigation的迁移库

RNNControllerFactory.m 1.7KB

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