Advertisement
Guest User

Untitled

a guest
May 27th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 KB | None | 0 0
  1. UIViewController *viewController = yourRootViewController;
  2.  
  3. NSMutableArray *array = [NSMutableArray array];
  4. while (viewController.modalViewController) {
  5. [array addObject:viewController];
  6. viewController = viewController.modalViewController;
  7. }
  8.  
  9. for (int i = 0; i < array.count; i++) {
  10. UIViewController *viewController = array[array.count-1-i];
  11. [viewController dismissModalViewControllerAnimated:NO];
  12. }
  13.  
  14. // ViewControllerA.m
  15. - (void)presentViewB {
  16. ViewControllerB *viewControllerB = [[ViewControllerB alloc] init];
  17. UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewControllerB];
  18.  
  19. navigationController.modalPresentationStyle = UIModalPresentationFormSheet;
  20. [self presentViewController:navigationController animated:YES completion:nil];
  21. }
  22.  
  23. //ViewControllerB.m
  24. - (void)presentViewC {
  25. ViewControllerC *viewControllerC = [[ViewControllerC alloc] init];
  26.  
  27. // Custom presenter method to handle setting up dismiss and snapshotting
  28. // I use this in a menu that can present many VC's so I centralized this part.
  29. [self presentViewControllerForModalDismissal:viewControllerC];
  30. }
  31.  
  32. #pragma mark - Modal Presentation Helper functions
  33. - (void)presentViewControllerForModalDismissal:(UIViewController*)viewControllerToPresent {
  34. UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewControllerToPresent];
  35. navigationController.modalPresentationStyle = UIModalPresentationFormSheet;
  36.  
  37. // Ensure that anything we are trying to present with this method has a dismissBlock since I don't want to force everything to inherit from some base class.
  38. NSAssert([viewControllerToPresent respondsToSelector:NSSelectorFromString(@"dismissBlock")], @"ViewControllers presented through this function must have a dismissBlock property of type (void)(^)()");
  39. [viewControllerToPresent setValue:[self getDismissalBlock] forKey:@"dismissBlock"];
  40.  
  41. [self presentViewController:navigationController animated:YES completion:^{
  42. // We want the presented view and this modal menu to dismiss simultaneous. The animation looks weird and immediately becomes the menu again when dismissing.
  43. // So we are snapshotting the presented view and adding it as a subview so you won't see the menu again when dismissing.
  44. UIView *snapshot = [navigationController.view snapshotViewAfterScreenUpdates:NO];
  45. [self.navigationController.view addSubview:snapshot];
  46. [snapshot autoPinEdgesToSuperviewEdges];
  47. }];
  48. }
  49.  
  50. - (void(^)()) getDismissalBlock {
  51. __weak __typeof(self) weakSelf = self;
  52. void(^dismissBlock)() = ^{
  53. __typeof(self) blockSafeSelf = weakSelf;
  54. [blockSafeSelf.navigationController.presentingViewController dismissViewControllerAnimated:YES completion:nil];
  55. };
  56.  
  57. return dismissBlock;
  58. }
  59.  
  60. // ViewControllerC.h
  61. @interface ViewControllerC : UIViewController
  62. @property (nonatomic, copy) void (^dismissBlock)(void);
  63. @end
  64.  
  65. //ViewControllerC.m
  66. // Make an method to handle dismissal that is called by button press or whatever logic makes sense.
  67. - (void)closeButtonPressed {
  68. if (_dismissBlock) {// If the dismissblock property was set, let the block handle dismissing
  69. _dismissBlock();
  70. return;
  71. }
  72.  
  73. // Leaving this here simply allows the viewController to be presented modally as the base as well or allow the presenter to handle it with a block.
  74. [self dismissViewControllerAnimated:YES completion:nil];
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement