Skip to content

Commit

Permalink
Caching ViewControllers
Browse files Browse the repository at this point in the history
  • Loading branch information
hartlco committed Jul 16, 2013
1 parent ae1510a commit b1bdaed
Show file tree
Hide file tree
Showing 7 changed files with 883 additions and 393 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,7 @@ build/
*.dep
# Cocoapods
Pods
Podfile.lock
Podfile.lock
# AppCode specific files
.idea/
*.iml
6 changes: 4 additions & 2 deletions MHCustomTabBarController/MHCustomTabBarController.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@

@interface MHCustomTabBarController : UIViewController

@property(weak,nonatomic)UIViewController *currentViewController;
@property (strong, nonatomic) NSString *currentIdentifier;
@property(weak,nonatomic)UIViewController *destinationViewController;
@property (strong, nonatomic) NSString *destinationIdentifier;
@property (strong, nonatomic) NSMutableDictionary *viewControllers;
@property (strong, nonatomic) UIViewController *oldViewController;
@property (weak, nonatomic) IBOutlet UIView *container;
@property (weak, nonatomic) IBOutlet UIView *buttonView;

Expand Down
20 changes: 14 additions & 6 deletions MHCustomTabBarController/MHCustomTabBarController.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ - (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

_viewControllers = [NSMutableDictionary dictionary];
}

-(void) viewWillAppear:(BOOL)animated
Expand Down Expand Up @@ -68,8 +70,7 @@ - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInte

}

- (BOOL)shouldAutorotateToInterfaceOrientation:
(UIInterfaceOrientation)interfaceOrientation {
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}

Expand All @@ -78,6 +79,13 @@ - (BOOL)shouldAutorotateToInterfaceOrientation:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

_oldViewController = _destinationViewController;

//if view controller isn't already contained in the viewControllers-Dictionary
if (![[_viewControllers allKeys] containsObject:segue.identifier]) {
[_viewControllers setObject:segue.destinationViewController forKey:segue.identifier];
}

for (UIView *subview in _buttonView.subviews) {
if ([subview isKindOfClass:[UIButton class]]) {
[((UIButton *)subview) setSelected:NO];
Expand All @@ -86,14 +94,14 @@ -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

UIButton *button = (UIButton *)sender;
[button setSelected:YES];
self.currentIdentifier = segue.identifier;
self.currentViewController = segue.destinationViewController;
self.destinationIdentifier = segue.identifier;
self.destinationViewController = [_viewControllers objectForKey:self.destinationIdentifier];


}

- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender {
if ([self.currentIdentifier isEqual:identifier]) {
if ([self.destinationIdentifier isEqual:identifier]) {
//Dont perform segue, if visible ViewController is already the destination ViewController
return NO;
}
Expand All @@ -105,7 +113,7 @@ - (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sende

- (void)didReceiveMemoryWarning {
for (UIViewController *vc in self.childViewControllers) {
if (![vc isEqual:self.currentViewController]) {
if (![vc isEqual:self.destinationViewController]) {
[vc willMoveToParentViewController:nil];
[vc removeFromParentViewController];
}
Expand Down
11 changes: 8 additions & 3 deletions MHCustomTabBarController/MHTabBarSegue.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,20 @@
@implementation MHTabBarSegue
- (void) perform {
MHCustomTabBarController *tabBarViewController = (MHCustomTabBarController *)self.sourceViewController;
UIViewController *destinationViewController = (UIViewController *) self.destinationViewController;
UIViewController *destinationViewController = (UIViewController *) tabBarViewController.destinationViewController;

//remove old viewController
if (tabBarViewController.oldViewController) {
[tabBarViewController.oldViewController willMoveToParentViewController:nil];
[tabBarViewController.oldViewController.view removeFromSuperview];
[tabBarViewController.oldViewController removeFromParentViewController];
}


destinationViewController.view.frame = tabBarViewController.container.bounds;
[tabBarViewController addChildViewController:destinationViewController];
[tabBarViewController.container addSubview:destinationViewController.view];
[destinationViewController didMoveToParentViewController:tabBarViewController];

tabBarViewController.currentViewController = destinationViewController;
}

@end
Loading

0 comments on commit b1bdaed

Please sign in to comment.