react-native-navigation的迁移库

RNNModalManager.m 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #import "RNNModalManager.h"
  2. #import "RNNRootViewController.h"
  3. @implementation RNNModalManager {
  4. RNNStore *_store;
  5. }
  6. -(instancetype)initWithStore:(RNNStore*)store {
  7. self = [super init];
  8. _store = store;
  9. return self;
  10. }
  11. -(void)waitForContentToAppearAndThen:(SEL)nameOfSelector {
  12. [[NSNotificationCenter defaultCenter] addObserver:self
  13. selector:nameOfSelector
  14. name: @"RCTContentDidAppearNotification"
  15. object:nil];
  16. }
  17. -(void)showModalAfterLoad:(NSDictionary*)notif {
  18. [[NSNotificationCenter defaultCenter] removeObserver:self name:@"RCTContentDidAppearNotification" object:nil];
  19. UIViewController *topVC = [self topPresentedVC];
  20. [topVC presentViewController:self.toVC animated:YES completion:nil];
  21. }
  22. //-(void)prepareShowModal{
  23. //
  24. //}
  25. -(void)showModal:(UIViewController *)viewController {
  26. self.toVC = viewController;
  27. // [self prepareShowModal]
  28. [self waitForContentToAppearAndThen:@selector(showModalAfterLoad:)];
  29. }
  30. -(void)dismissModal:(NSString *)containerId {
  31. [[_store pendingModalIdsToDismiss] addObject:containerId];
  32. [self removePendingNextModalIfOnTop];
  33. }
  34. -(void)dismissAllModals {
  35. UIViewController *root = UIApplication.sharedApplication.delegate.window.rootViewController;
  36. [root dismissViewControllerAnimated:YES completion:nil];
  37. [[_store pendingModalIdsToDismiss] removeAllObjects];
  38. }
  39. #pragma mark - private
  40. -(void)removePendingNextModalIfOnTop {
  41. NSString *containerId = [[_store pendingModalIdsToDismiss] lastObject];
  42. UIViewController *modalToDismiss = [_store findContainerForId:containerId];
  43. if(!modalToDismiss) {
  44. return;
  45. }
  46. UIViewController* topPresentedVC = [self topPresentedVC];
  47. if (modalToDismiss == topPresentedVC || [[topPresentedVC childViewControllers] containsObject:modalToDismiss]) {
  48. [modalToDismiss dismissViewControllerAnimated:YES completion:^{
  49. [[_store pendingModalIdsToDismiss] removeObject:containerId];
  50. [self removePendingNextModalIfOnTop];
  51. }];
  52. }
  53. }
  54. -(UIViewController*)topPresentedVC {
  55. UIViewController *root = UIApplication.sharedApplication.delegate.window.rootViewController;
  56. while(root.presentedViewController) {
  57. root = root.presentedViewController;
  58. }
  59. return root;
  60. }
  61. @end