From 08cd2c5dcc0e10ff8a1b60d730b610461fc451ad Mon Sep 17 00:00:00 2001 From: Robert Buskens Date: Wed, 9 Jul 2014 06:51:07 -0500 Subject: [PATCH] =?UTF-8?q?Basics=20for=20state=20preservation=20/=20resto?= =?UTF-8?q?ration=20setup.=20Not=20complete.=20Workaround=20in=20SWRevealC?= =?UTF-8?q?ontroller,=20toggle=20rearView=20twice=20as=20a=20work=20around?= =?UTF-8?q?.=20The=20state=20of=20the=20menu=20works.=20the=20framework=20?= =?UTF-8?q?methods=20are=20getting=20called=20in=20the=20other=20pages,=20?= =?UTF-8?q?but=20haven=E2=80=99t=20debugged=20them.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AppDelegate.m | 14 ++++ .../ColorViewController.m | 26 ++++++++ .../MapViewController.m | 23 +++++++ .../MenuViewController.m | 23 +++++++ .../en.lproj/MainStoryboard-iPad.storyboard | 16 ++--- .../SWRevealViewController.m | 65 +++++++++++++++++++ 6 files changed, 159 insertions(+), 8 deletions(-) diff --git a/RevealControllerStoryboardExample2/RevealControllerStoryboardExample2/AppDelegate.m b/RevealControllerStoryboardExample2/RevealControllerStoryboardExample2/AppDelegate.m index 849fb72..55dc092 100755 --- a/RevealControllerStoryboardExample2/RevealControllerStoryboardExample2/AppDelegate.m +++ b/RevealControllerStoryboardExample2/RevealControllerStoryboardExample2/AppDelegate.m @@ -43,4 +43,18 @@ - (void)applicationWillTerminate:(UIApplication *)application // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. } +#pragma mark state preservation / restoration +- (BOOL) application:(UIApplication *)application shouldSaveApplicationState:(NSCoder *)coder { + return YES; +} + +- (BOOL) application:(UIApplication *)application shouldRestoreApplicationState:(NSCoder *)coder { + return YES; +} + +- (BOOL) application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions { + + [self.window makeKeyAndVisible]; + return YES; +} @end diff --git a/RevealControllerStoryboardExample2/RevealControllerStoryboardExample2/ColorViewController.m b/RevealControllerStoryboardExample2/RevealControllerStoryboardExample2/ColorViewController.m index 99ba03a..d024cf4 100755 --- a/RevealControllerStoryboardExample2/RevealControllerStoryboardExample2/ColorViewController.m +++ b/RevealControllerStoryboardExample2/RevealControllerStoryboardExample2/ColorViewController.m @@ -26,4 +26,30 @@ - (void)viewDidLoad _label.textColor = _color; } +#pragma mark state preservation / restoration +- (void)encodeRestorableStateWithCoder:(NSCoder *)coder { + NSLog(@"%s", __PRETTY_FUNCTION__); + + // TODO save what you need here + [coder encodeObject: _label.text forKey: @"lableText"]; + + [super encodeRestorableStateWithCoder:coder]; +} + +- (void)decodeRestorableStateWithCoder:(NSCoder *)coder { + NSLog(@"%s", __PRETTY_FUNCTION__); + + // TODO restore what you need here + _label.text = [coder decodeObjectForKey: @"labelText"]; + + [super decodeRestorableStateWithCoder:coder]; +} + +- (void)applicationFinishedRestoringState { + NSLog(@"%s", __PRETTY_FUNCTION__); + + // TODO call whatever function you need to visually restore + +} + @end diff --git a/RevealControllerStoryboardExample2/RevealControllerStoryboardExample2/MapViewController.m b/RevealControllerStoryboardExample2/RevealControllerStoryboardExample2/MapViewController.m index 482f026..e1bf9c6 100755 --- a/RevealControllerStoryboardExample2/RevealControllerStoryboardExample2/MapViewController.m +++ b/RevealControllerStoryboardExample2/RevealControllerStoryboardExample2/MapViewController.m @@ -23,4 +23,27 @@ - (void)viewDidLoad [self.navigationController.navigationBar addGestureRecognizer: self.revealViewController.panGestureRecognizer]; } +#pragma mark state preservation / restoration +- (void)encodeRestorableStateWithCoder:(NSCoder *)coder { + NSLog(@"%s", __PRETTY_FUNCTION__); + + // TODO save what you need here + + [super encodeRestorableStateWithCoder:coder]; +} + +- (void)decodeRestorableStateWithCoder:(NSCoder *)coder { + NSLog(@"%s", __PRETTY_FUNCTION__); + + // TODO restore what you need here + + [super decodeRestorableStateWithCoder:coder]; +} + +- (void)applicationFinishedRestoringState { + NSLog(@"%s", __PRETTY_FUNCTION__); + + // TODO call whatever function you need to visually restore +} + @end diff --git a/RevealControllerStoryboardExample2/RevealControllerStoryboardExample2/MenuViewController.m b/RevealControllerStoryboardExample2/RevealControllerStoryboardExample2/MenuViewController.m index c3e0512..072b6a9 100755 --- a/RevealControllerStoryboardExample2/RevealControllerStoryboardExample2/MenuViewController.m +++ b/RevealControllerStoryboardExample2/RevealControllerStoryboardExample2/MenuViewController.m @@ -99,4 +99,27 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N return cell; } +#pragma mark state preservation / restoration +- (void)encodeRestorableStateWithCoder:(NSCoder *)coder { + NSLog(@"%s", __PRETTY_FUNCTION__); + + // TODO save what you need here + + [super encodeRestorableStateWithCoder:coder]; +} + +- (void)decodeRestorableStateWithCoder:(NSCoder *)coder { + NSLog(@"%s", __PRETTY_FUNCTION__); + + // TODO restore what you need here + + [super decodeRestorableStateWithCoder:coder]; +} + +- (void)applicationFinishedRestoringState { + NSLog(@"%s", __PRETTY_FUNCTION__); + + // TODO call whatever function you need to visually restore +} + @end diff --git a/RevealControllerStoryboardExample2/RevealControllerStoryboardExample2/en.lproj/MainStoryboard-iPad.storyboard b/RevealControllerStoryboardExample2/RevealControllerStoryboardExample2/en.lproj/MainStoryboard-iPad.storyboard index 5e5cfe2..c9deb5f 100755 --- a/RevealControllerStoryboardExample2/RevealControllerStoryboardExample2/en.lproj/MainStoryboard-iPad.storyboard +++ b/RevealControllerStoryboardExample2/RevealControllerStoryboardExample2/en.lproj/MainStoryboard-iPad.storyboard @@ -8,7 +8,7 @@ - + @@ -26,8 +26,8 @@ - - + + @@ -117,7 +117,7 @@ - + @@ -147,7 +147,7 @@ - + @@ -162,7 +162,7 @@ - + @@ -189,8 +189,8 @@ - - + + diff --git a/SWRevealViewController/SWRevealViewController.m b/SWRevealViewController/SWRevealViewController.m index 802ccc5..84bc5fc 100755 --- a/SWRevealViewController/SWRevealViewController.m +++ b/SWRevealViewController/SWRevealViewController.m @@ -1643,6 +1643,71 @@ - (void)loadStoryboardControllers } } +#pragma mark state preservation / restoration ++ (UIViewController*) viewControllerWithRestorationIdentifierPath:(NSArray*) identifierComponents coder:(NSCoder*)coder { + NSLog(@"%s", __PRETTY_FUNCTION__); + + SWRevealViewController* vc; + UIStoryboard* sb = [coder decodeObjectForKey:UIStateRestorationViewControllerStoryboardKey]; + + if (sb) { + vc = (SWRevealViewController*)[sb instantiateViewControllerWithIdentifier:@"SWRevealViewController"]; + vc.restorationIdentifier = [identifierComponents lastObject]; + vc.restorationClass = [SWRevealViewController class]; + } + return vc; +} + +- (void)encodeRestorableStateWithCoder:(NSCoder *)coder { + + // this will include the other view controllers in state encoding and decoding + if (_rearViewController) { + [coder encodeObject: _rearViewController forKey: @"rearViewController"]; + } + + if (_frontViewController) { + [coder encodeObject: _frontViewController forKey: @"frontViewController"]; + } + + if (_rightViewController) { + [coder encodeObject: _rightViewController forKey: @"rightViewController"]; + } + + // ??? What's needed to restore what position the views are in + [coder encodeInt: _frontViewPosition forKey: @"frontViewPosition"]; + [coder encodeInt: _rearViewPosition forKey: @"rearViewPosition"]; + [coder encodeInt: _rightViewPosition forKey: @"rightViewPosition"]; + + [super encodeRestorableStateWithCoder:coder]; +} + +- (void)decodeRestorableStateWithCoder:(NSCoder *)coder { + + // ??? What's needed to restore what position the views are in + _frontViewPosition = [coder decodeIntForKey: @"frontViewPosition"]; + _rearViewPosition = [coder decodeIntForKey: @"rearViewPosition"]; + _rightViewPosition = [coder decodeIntForKey: @"rightViewPosition"]; + + [super decodeRestorableStateWithCoder:coder]; +} + +- (void)applicationFinishedRestoringState { + + // ??? Get the views to where they should be + + [self _setFrontViewPosition: _frontViewPosition withDuration:0.0]; + + [self revealToggleAnimated: NO]; + [self revealToggleAnimated: NO]; + +// if (_rearViewPosition != FrontViewPositionNone) { +// [self revealToggleAnimated: NO]; +// } +// +// if (_rightViewPosition >= FrontViewPositionLeft) { +// [self rightRevealToggleAnimated: NO]; +// } +} @end