Created
May 16, 2014 07:15
-
-
Save mariancerny/a168aa4e7e61777e1037 to your computer and use it in GitHub Desktop.
Wrapper for a modal view controller that should be assigned to rootViewController of a new UIWindow, so that the modal view controller can be presented out of the application flow (based for example on an external event).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@protocol TransparentViewControllerDelegate; | |
@interface TransparentViewController : UIViewController | |
- (instancetype)initWithRootViewController:(UIViewController *)rootViewController; | |
@property (nonatomic, weak) id<TransparentViewControllerDelegate> delegate; | |
@end | |
@protocol TransparentViewControllerDelegate <NSObject> | |
- (void)transparentViewControllerDidDismissRootViewController:(TransparentViewController *)transparentViewController; | |
@end | |
@interface TransparentViewController () | |
@property (nonatomic, strong) UIViewController *rootViewController; | |
@property (nonatomic) BOOL didAlreadyAppear; | |
@end | |
@implementation TransparentViewController | |
- (instancetype)initWithRootViewController:(UIViewController *)rootViewController | |
{ | |
self = [super init]; | |
if (self) { | |
_rootViewController = rootViewController; | |
} | |
return self; | |
} | |
- (void)viewDidAppear:(BOOL)animated | |
{ | |
[super viewDidAppear:animated]; | |
if (self.didAlreadyAppear) { | |
[self.view.window resignKeyWindow]; | |
self.view.window.hidden = YES; | |
if ([self.delegate respondsToSelector:@selector(transparentViewControllerDidDismissRootViewController:)]) | |
[self.delegate transparentViewControllerDidDismissRootViewController:self]; | |
} else { | |
if (self.rootViewController) | |
[self presentViewController:self.rootViewController animated:YES completion:nil]; | |
self.rootViewController = nil; | |
} | |
self.didAlreadyAppear = YES; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage is as follows: