Skip to content

Commit 869d49b

Browse files
committed
Demonstrate cached vs. non-cached view controllers
1 parent fcd2824 commit 869d49b

File tree

2 files changed

+31
-12
lines changed

2 files changed

+31
-12
lines changed

Examples/TransitionFun/README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,33 @@ Have fun with transitions with TransitionFun! This is a universal app that has a
88

99
There is a lot of plumbing in the project for setting up the table and changing the sliding view controller's delegate. We'll point out some of the more interesting parts here.
1010

11+
### Cached Top View Controllers
12+
13+
You'll notice that the transitions view controller table will remember the previously selected transition when switching between the it and the settings view controller. This is accomplished by caching the transitions view controller in the menu.
14+
15+
The sliding view controller is initialized in storyboards with the transitions view controller set as the `topViewController` and the menu view controller set as the `underLeftViewController`. We want to keep a reference to the transitions view controller from the menu view controller, so in `viewDidLoad:`
16+
17+
```objc
18+
// self.transitionsNavigationController will keep a strong reference
19+
self.transitionsNavigationController = (UINavigationController *)self.slidingViewController.topViewController;
20+
```
21+
22+
When the user selects "Settings", we'll change the `topViewController`. In `tableView:didSelectRowAtIndexPath:`
23+
24+
```objc
25+
self.slidingViewController.topViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"MESettingsNavigationController"];
26+
```
27+
28+
The `topViewController` is now replaced with a new instance of the settings view controller. We still have a reference to the transitions view controller that we kept in `viewDidLoad:`, so we can use that instance again when the user selects "Transitions"
29+
30+
```objc
31+
self.slidingViewController.topViewController = self.transitionsNavigationController;
32+
```
33+
34+
This replaces the settings view controller, but since we don't care about losing its state we'll let that instance be released by the system.
35+
36+
Re-using the same instance will prevent it from losing state. It's your responsibility to decide if a view controller should be cached or not while balancing memory usage.
37+
1138
### Custom Transitions
1239

1340
The `METransitionsViewController` is a bit more complex than your view controller would need to be to customize a transition. The important thing to see is that each custom transition is implemented in its own object that conforms to `ECSlidingViewControllerDelegate`. This cleans the view controller up and is better for reusability, and for this project it makes it easy to switch between transitions by changing the delegate.

Examples/TransitionFun/TransitionFun/MEMenuViewController.m

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
@interface MEMenuViewController ()
2828
@property (nonatomic, strong) NSArray *menuItems;
2929
@property (nonatomic, strong) UINavigationController *transitionsNavigationController;
30-
@property (nonatomic, strong) UINavigationController *settingsNavigationController;
3130
@end
3231

3332
@implementation MEMenuViewController
@@ -56,14 +55,6 @@ - (NSArray *)menuItems {
5655
return _menuItems;
5756
}
5857

59-
- (UINavigationController *)settingsNavigationController {
60-
if (_settingsNavigationController) return _settingsNavigationController;
61-
62-
_settingsNavigationController = [self.storyboard instantiateViewControllerWithIdentifier:@"MESettingsNavigationController"];
63-
64-
return _settingsNavigationController;
65-
}
66-
6758
#pragma mark - UITableViewDataSource
6859

6960
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
@@ -92,11 +83,12 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
9283
// dynamically so everything needs to start in a consistent state.
9384
self.slidingViewController.topViewController.view.layer.transform = CATransform3DMakeScale(1, 1, 1);
9485

95-
if ([menuItem isEqualToString:@"Transitions"] && self.slidingViewController.topViewController != self.transitionsNavigationController) {
86+
if ([menuItem isEqualToString:@"Transitions"]) {
9687
self.slidingViewController.topViewController = self.transitionsNavigationController;
97-
} else if ([menuItem isEqualToString:@"Settings"] && self.slidingViewController.topViewController != self.settingsNavigationController) {
98-
self.slidingViewController.topViewController = self.settingsNavigationController;
88+
} else if ([menuItem isEqualToString:@"Settings"]) {
89+
self.slidingViewController.topViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"MESettingsNavigationController"];
9990
}
91+
10092

10193
[self.slidingViewController resetTopViewAnimated:YES];
10294
}

0 commit comments

Comments
 (0)