Last active
May 11, 2016 18:43
-
-
Save florieger/1309348 to your computer and use it in GitHub Desktop.
View Controller Lifecycle Management [Xibless Version]
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
@interface ACViewController : UIViewController | |
{ | |
UIViewController* _childViewController; | |
UIScrollView* _scrollView; | |
UIButton* _button; | |
int _counter; | |
NSString* _text; | |
CGPoint _lastScrollViewPosition; | |
} | |
@property (assign, nonatomic) BOOL isButtonEnabled; | |
@end |
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
#import "ACViewController.h" | |
@implementation ACViewController | |
#pragma mark - | |
#pragma mark Controller Lifecycle | |
// Called once the view controller is initialized in your code | |
- (id)init | |
{ | |
self = [super init]; | |
if (self) { | |
// Initialize iVars that live and die with your controller | |
_counter = 0; | |
_lastScrollViewPosition = CGPointMake(0, 0); | |
// Initialize child view controllers | |
_childViewController = [[UIViewController alloc] init]; | |
} | |
return self; | |
} | |
#pragma mark - | |
#pragma mark View Lifecycle | |
// Called multiple times whenever the view loads to recreate the view without a XIB file. | |
// This method only needs to be implemented in the root class of you inheritance hierarchy. | |
- (void)loadView | |
{ | |
// Important, don't call [super loadView]. | |
// See Apple's View Controller Programming Guide (Custom View Controllers) for details. | |
UIView* baseView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]]; | |
self.view = baseView; | |
} | |
// Called multiple times whenever the view loads. | |
- (void)viewDidLoad | |
{ | |
[super viewDidLoad]; | |
// Modify the base view | |
self.view.frame = CGRectMake(0, 0, 600, 400); | |
self.view.backgroundColor = [UIColor redColor]; | |
// Add scrollview | |
_scrollView = [[UIScrollView alloc] initWithFrame:self.view.frame]; | |
[self.view addSubview:_scrollView]; | |
// Restore state of scrollview | |
_scrollView.contentOffset = _lastScrollViewPosition; | |
// Add button | |
_button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; | |
_button.frame = CGRectMake(10, 10, 200, 50); | |
[self.view addSubview:_button]; | |
// Add chidlview controller to view hierachy | |
// willMoveToParantViewController gets called automatically | |
[self addChildViewController:_childViewController]; | |
[self.view addSubview:_childViewController.view]; | |
[_childViewController didMoveToParentViewController:self]; | |
// Initialize iVars that life and die with your view | |
_text = @"Hello World"; | |
} | |
// Called multiple times whenever the view is going to appear. | |
- (void)viewWillAppear:(BOOL)animated | |
{ | |
[super viewWillAppear:(BOOL)animated]; | |
_button.enabled = self.isButtonEnabled; | |
} | |
// Called on memory warnings. | |
- (void)didReceiveMemoryWarning | |
{ | |
// Check that the view isn't visible at moment | |
if ([self isViewLoaded] && [self.view window] == nil) | |
{ | |
// Release all views that you recreate in loadView and viewDidLoad | |
// Important: Set self.view to nil only if you created it. | |
self.view = nil; | |
_scrollView = nil; | |
// Remove chidlview controller from view hierachy | |
// didMoveToParantViewController gets called automatically | |
[_childViewController willMoveToParentViewController:nil]; | |
[_childViewController.view removeFromSuperview]; | |
[_childViewController removeFromParentViewController]; | |
// Release iVars that live and die with your view; | |
_text = nil; | |
} | |
// Call super at the end of the method | |
[super didReceiveMemoryWarning]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment