react-native-navigation的迁移库

RNNModalManager.m 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #import "RNNModalManager.h"
  2. @implementation RNNModalManager {
  3. RNNStore *_store;
  4. }
  5. -(instancetype)initWithStore:(RNNStore*)store {
  6. self = [super init];
  7. _store = store;
  8. return self;
  9. }
  10. -(void)showModal:(UIViewController *)viewController {
  11. UIViewController *topVC = [self topPresentedVC];
  12. [topVC presentViewController:viewController animated:YES completion:nil];
  13. }
  14. -(void)dismissModal:(NSString *)containerId {
  15. [[_store pendingModalIdsToDismiss] addObject:containerId];
  16. [self removePendingNextModalIfOnTop];
  17. }
  18. -(void)dismissAllModals {
  19. UIViewController *root = UIApplication.sharedApplication.delegate.window.rootViewController;
  20. [root dismissViewControllerAnimated:YES completion:nil];
  21. [[_store pendingModalIdsToDismiss] removeAllObjects];
  22. }
  23. #pragma mark - private
  24. -(void)removePendingNextModalIfOnTop {
  25. NSString *containerId = [[_store pendingModalIdsToDismiss] lastObject];
  26. UIViewController *modalToDismiss = [_store findContainerForId:containerId];
  27. if(!modalToDismiss) {
  28. return;
  29. }
  30. UIViewController* topPresentedVC = [self topPresentedVC];
  31. if (modalToDismiss == topPresentedVC || [[topPresentedVC childViewControllers] containsObject:modalToDismiss]) {
  32. [modalToDismiss dismissViewControllerAnimated:YES completion:^{
  33. [[_store pendingModalIdsToDismiss] removeObject:containerId];
  34. [self removePendingNextModalIfOnTop];
  35. }];
  36. }
  37. }
  38. -(UIViewController*)topPresentedVC {
  39. UIViewController *root = UIApplication.sharedApplication.delegate.window.rootViewController;
  40. while(root.presentedViewController) {
  41. root = root.presentedViewController;
  42. }
  43. return root;
  44. }
  45. @end