Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- UIViewController *viewController = yourRootViewController;
- NSMutableArray *array = [NSMutableArray array];
- while (viewController.modalViewController) {
- [array addObject:viewController];
- viewController = viewController.modalViewController;
- }
- for (int i = 0; i < array.count; i++) {
- UIViewController *viewController = array[array.count-1-i];
- [viewController dismissModalViewControllerAnimated:NO];
- }
- // ViewControllerA.m
- - (void)presentViewB {
- ViewControllerB *viewControllerB = [[ViewControllerB alloc] init];
- UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewControllerB];
- navigationController.modalPresentationStyle = UIModalPresentationFormSheet;
- [self presentViewController:navigationController animated:YES completion:nil];
- }
- //ViewControllerB.m
- - (void)presentViewC {
- ViewControllerC *viewControllerC = [[ViewControllerC alloc] init];
- // Custom presenter method to handle setting up dismiss and snapshotting
- // I use this in a menu that can present many VC's so I centralized this part.
- [self presentViewControllerForModalDismissal:viewControllerC];
- }
- #pragma mark - Modal Presentation Helper functions
- - (void)presentViewControllerForModalDismissal:(UIViewController*)viewControllerToPresent {
- UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewControllerToPresent];
- navigationController.modalPresentationStyle = UIModalPresentationFormSheet;
- // 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.
- NSAssert([viewControllerToPresent respondsToSelector:NSSelectorFromString(@"dismissBlock")], @"ViewControllers presented through this function must have a dismissBlock property of type (void)(^)()");
- [viewControllerToPresent setValue:[self getDismissalBlock] forKey:@"dismissBlock"];
- [self presentViewController:navigationController animated:YES completion:^{
- // We want the presented view and this modal menu to dismiss simultaneous. The animation looks weird and immediately becomes the menu again when dismissing.
- // So we are snapshotting the presented view and adding it as a subview so you won't see the menu again when dismissing.
- UIView *snapshot = [navigationController.view snapshotViewAfterScreenUpdates:NO];
- [self.navigationController.view addSubview:snapshot];
- [snapshot autoPinEdgesToSuperviewEdges];
- }];
- }
- - (void(^)()) getDismissalBlock {
- __weak __typeof(self) weakSelf = self;
- void(^dismissBlock)() = ^{
- __typeof(self) blockSafeSelf = weakSelf;
- [blockSafeSelf.navigationController.presentingViewController dismissViewControllerAnimated:YES completion:nil];
- };
- return dismissBlock;
- }
- // ViewControllerC.h
- @interface ViewControllerC : UIViewController
- @property (nonatomic, copy) void (^dismissBlock)(void);
- @end
- //ViewControllerC.m
- // Make an method to handle dismissal that is called by button press or whatever logic makes sense.
- - (void)closeButtonPressed {
- if (_dismissBlock) {// If the dismissblock property was set, let the block handle dismissing
- _dismissBlock();
- return;
- }
- // 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.
- [self dismissViewControllerAnimated:YES completion:nil];
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement