react-native-navigation的迁移库

RNNModalManager.m 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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)showModal:(UIViewController *)viewController {
  12. UIViewController *topVC = [self topPresentedVC];
  13. [topVC presentViewController:viewController animated:YES completion:nil];
  14. }
  15. -(void)dismissModal:(NSString *)containerId {
  16. [[_store pendingModalIdsToDismiss] addObject:containerId];
  17. [self removePendingNextModalIfOnTop];
  18. }
  19. -(void)dismissAllModals {
  20. UIViewController *root = UIApplication.sharedApplication.delegate.window.rootViewController;
  21. [root dismissViewControllerAnimated:YES completion:nil];
  22. [[_store pendingModalIdsToDismiss] removeAllObjects];
  23. }
  24. #pragma mark - private
  25. -(void)removePendingNextModalIfOnTop {
  26. NSString *containerId = [[_store pendingModalIdsToDismiss] lastObject];
  27. UIViewController *modalToDismiss = [_store findContainerForId:containerId];
  28. if(!modalToDismiss) {
  29. return;
  30. }
  31. UIViewController* topPresentedVC = [self topPresentedVC];
  32. if (modalToDismiss == topPresentedVC || [[topPresentedVC childViewControllers] containsObject:modalToDismiss]) {
  33. [modalToDismiss dismissViewControllerAnimated:YES completion:^{
  34. [[_store pendingModalIdsToDismiss] removeObject:containerId];
  35. [self removePendingNextModalIfOnTop];
  36. }];
  37. }
  38. }
  39. -(UIViewController*)topPresentedVC {
  40. UIViewController *root = UIApplication.sharedApplication.delegate.window.rootViewController;
  41. while(root.presentedViewController) {
  42. root = root.presentedViewController;
  43. }
  44. return root;
  45. }
  46. @end