diff --git a/KitchenSink/ExampleFiles/MMAppDelegate.m b/KitchenSink/ExampleFiles/MMAppDelegate.m index 746348e4..17904a11 100644 --- a/KitchenSink/ExampleFiles/MMAppDelegate.m +++ b/KitchenSink/ExampleFiles/MMAppDelegate.m @@ -29,10 +29,14 @@ #import -@implementation MMAppDelegate +@interface MMAppDelegate () -- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions -{ +@property (nonatomic,strong) MMDrawerController * drawerController; + +@end + +@implementation MMAppDelegate +-(BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ UIViewController * leftSideDrawerViewController = [[MMExampleLeftSideDrawerViewController alloc] init]; UIViewController * centerViewController = [[MMExampleCenterTableViewController alloc] initWithStyle:UITableViewStyleGrouped]; @@ -40,28 +44,36 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:( UIViewController * rightSideDrawerViewController = [[MMExampleRightSideDrawerViewController alloc] init]; UINavigationController * navigationController = [[UINavigationController alloc] initWithRootViewController:centerViewController]; + [navigationController setRestorationIdentifier:@"MMExampleCenterNavigationControllerRestorationKey"]; - MMDrawerController * drawerController = [[MMDrawerController alloc] - initWithCenterViewController:navigationController - leftDrawerViewController:leftSideDrawerViewController - rightDrawerViewController:rightSideDrawerViewController]; - [drawerController setMaximumRightDrawerWidth:200.0]; - [drawerController setOpenDrawerGestureModeMask:MMOpenDrawerGestureModeAll]; - [drawerController setCloseDrawerGestureModeMask:MMCloseDrawerGestureModeAll]; + self.drawerController = [[MMDrawerController alloc] + initWithCenterViewController:navigationController + leftDrawerViewController:leftSideDrawerViewController + rightDrawerViewController:rightSideDrawerViewController]; + [self.drawerController setRestorationIdentifier:@"MMDrawer"]; + [self.drawerController setMaximumRightDrawerWidth:200.0]; + [self.drawerController setOpenDrawerGestureModeMask:MMOpenDrawerGestureModeAll]; + [self.drawerController setCloseDrawerGestureModeMask:MMCloseDrawerGestureModeAll]; - [drawerController + [self.drawerController setDrawerVisualStateBlock:^(MMDrawerController *drawerController, MMDrawerSide drawerSide, CGFloat percentVisible) { MMDrawerControllerDrawerVisualStateBlock block; block = [[MMExampleDrawerVisualStateManager sharedManager] - drawerVisualStateBlockForDrawerSide:drawerSide]; + drawerVisualStateBlockForDrawerSide:drawerSide]; if(block){ block(drawerController, drawerSide, percentVisible); } }]; - self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; - [self.window setRootViewController:drawerController]; - // Override point for customization after application launch. + [self.window setRootViewController:self.drawerController]; + + return YES; +} + + +- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions +{ + self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; return YES; @@ -94,4 +106,31 @@ - (void)applicationWillTerminate:(UIApplication *)application // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. } +- (BOOL)application:(UIApplication *)application shouldSaveApplicationState:(NSCoder *)coder{ + return YES; +} + +- (BOOL)application:(UIApplication *)application shouldRestoreApplicationState:(NSCoder *)coder{ + return YES; +} + + +- (UIViewController *)application:(UIApplication *)application viewControllerWithRestorationIdentifierPath:(NSArray *)identifierComponents coder:(NSCoder *)coder +{ + NSString * key = [identifierComponents lastObject]; + if([key isEqualToString:@"MMDrawer"]){ + return self.window.rootViewController; + } + else if ([key isEqualToString:@"MMExampleCenterNavigationControllerRestorationKey"]) { + return ((MMDrawerController *)self.window.rootViewController).centerViewController; + } + else if ([key isEqualToString:@"MMExampleLeftSideDrawerController"]){ + return ((MMDrawerController *)self.window.rootViewController).leftDrawerViewController; + } + else if ([key isEqualToString:@"MMExampleRightSideDrawerController"]){ + return ((MMDrawerController *)self.window.rootViewController).rightDrawerViewController; + } + return nil; +} + @end diff --git a/KitchenSink/ExampleFiles/MMExampleCenterTableViewController.m b/KitchenSink/ExampleFiles/MMExampleCenterTableViewController.m index 6a995239..3f86c232 100644 --- a/KitchenSink/ExampleFiles/MMExampleCenterTableViewController.m +++ b/KitchenSink/ExampleFiles/MMExampleCenterTableViewController.m @@ -47,7 +47,7 @@ - (id)initWithStyle:(UITableViewStyle)style { self = [super initWithStyle:style]; if (self) { - // Custom initialization + [self setRestorationIdentifier:@"MMExampleCenterControllerRestorationKey"]; } return self; } diff --git a/KitchenSink/ExampleFiles/MMExampleLeftSideDrawerViewController.m b/KitchenSink/ExampleFiles/MMExampleLeftSideDrawerViewController.m index 010dab1f..cc52b977 100644 --- a/KitchenSink/ExampleFiles/MMExampleLeftSideDrawerViewController.m +++ b/KitchenSink/ExampleFiles/MMExampleLeftSideDrawerViewController.m @@ -28,6 +28,14 @@ @interface MMExampleLeftSideDrawerViewController () @implementation MMExampleLeftSideDrawerViewController +-(id)init{ + self = [super init]; + if(self){ + [self setRestorationIdentifier:@"MMExampleLeftSideDrawerController"]; + } + return self; +} + -(void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; NSLog(@"Left will appear"); diff --git a/KitchenSink/ExampleFiles/MMExampleRightSideDrawerViewController.m b/KitchenSink/ExampleFiles/MMExampleRightSideDrawerViewController.m index d67e301c..31cc3c7c 100644 --- a/KitchenSink/ExampleFiles/MMExampleRightSideDrawerViewController.m +++ b/KitchenSink/ExampleFiles/MMExampleRightSideDrawerViewController.m @@ -26,6 +26,13 @@ @interface MMExampleRightSideDrawerViewController () @end @implementation MMExampleRightSideDrawerViewController +-(id)init{ + self = [super init]; + if(self){ + [self setRestorationIdentifier:@"MMExampleRightSideDrawerController"]; + } + return self; +} -(void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; diff --git a/MMDrawerController/MMDrawerController.h b/MMDrawerController/MMDrawerController.h index 5d06d7b2..84534343 100644 --- a/MMDrawerController/MMDrawerController.h +++ b/MMDrawerController/MMDrawerController.h @@ -39,6 +39,9 @@ ## How Open/Close Gestures are handled Two gestures are added to every instance of a drawer controller, one for pan and one for touch. `MMDrawerController` is the delegate for each of the gesture recoginzers, and determines if a touch should be sent to the appropriate gesture when a touch is detected compared with the masks set for open and close gestures and the state of the drawer controller. + ## Integrating with State Restoration + In order to opt in to state restoration for `MMDrawerController`, you must set the `restorationIdentifier` of your drawer controller. Instances of your centerViewController, leftDrawerViewController and rightDrawerViewController must also be configured with their own `restorationIdentifier` (and optionally a restorationClass) if you intend for those to be restored as well. If your MMDrawerController had an open drawer when your app was sent to the background, that state will also be restored. + ## What this library doesn't do. This library is not meant for: - Top or bottom drawer views diff --git a/MMDrawerController/MMDrawerController.m b/MMDrawerController/MMDrawerController.m index fad9ee50..ce904307 100644 --- a/MMDrawerController/MMDrawerController.m +++ b/MMDrawerController/MMDrawerController.m @@ -75,6 +75,11 @@ return animation; } +static NSString *MMDrawerLeftDrawerKey = @"MMDrawerLeftDrawer"; +static NSString *MMDrawerRightDrawerKey = @"MMDrawerRightDrawer"; +static NSString *MMDrawerCenterKey = @"MMDrawerCenter"; +static NSString *MMDrawerOpenSideKey = @"MMDrawerOpenSide"; + @interface MMDrawerCenterContainerView : UIView @property (nonatomic,assign) MMDrawerOpenCenterInteractionMode centerInteractionMode; @property (nonatomic,assign) MMDrawerSide openSide; @@ -182,6 +187,46 @@ -(void)commonSetup{ [self setCenterHiddenInteractionMode:MMDrawerOpenCenterInteractionModeNavigationBarOnly]; } +#pragma mark - State Restoration +- (void)encodeRestorableStateWithCoder:(NSCoder *)coder{ + [super encodeRestorableStateWithCoder:coder]; + if (self.leftDrawerViewController){ + [coder encodeObject:self.leftDrawerViewController forKey:MMDrawerLeftDrawerKey]; + } + + if (self.rightDrawerViewController){ + [coder encodeObject:self.rightDrawerViewController forKey:MMDrawerRightDrawerKey]; + } + + if (self.centerViewController){ + [coder encodeObject:self.centerViewController forKey:MMDrawerCenterKey]; + } + + [coder encodeInteger:self.openSide forKey:MMDrawerOpenSideKey]; +} + +- (void)decodeRestorableStateWithCoder:(NSCoder *)coder{ + UIViewController *controller; + MMDrawerSide openside; + + [super decodeRestorableStateWithCoder:coder]; + + if ((controller = [coder decodeObjectForKey:MMDrawerLeftDrawerKey])){ + self.leftDrawerViewController = [coder decodeObjectForKey:MMDrawerLeftDrawerKey]; + } + + if ((controller = [coder decodeObjectForKey:MMDrawerRightDrawerKey])){ + self.rightDrawerViewController = controller; + } + + if ((controller = [coder decodeObjectForKey:MMDrawerCenterKey])){ + self.centerViewController = controller; + } + + if ((openside = [coder decodeIntegerForKey:MMDrawerOpenSideKey])){ + [self openDrawerSide:openside animated:false completion:nil]; + } +} #pragma mark - Open/Close methods -(void)toggleDrawerSide:(MMDrawerSide)drawerSide animated:(BOOL)animated completion:(void (^)(BOOL finished))completion{ NSParameterAssert(drawerSide!=MMDrawerSideNone);