Skip to content

View Controller LifeCycle

zjy edited this page Jul 13, 2016 · 1 revision

View Controller LifeCycle

Summary: Instantiated (from storyboard - many ways for this to happen which we’ll cover later)

	awakeFromNib //This method is sent to all objects that come out of a storyboard (including your Controller).
		     	 //you can think it is awake from storyboard

or
	- (instancetype)initWithNibName:(NSString *)name bundle:(NSBundle *)bundle


Anything that would go in your Controller’s init method would have to go in awakeFromNib also 
 
- (void)setup{}; 

- (void)awakeFromNib { [self setup]; }

// UIViewController’s designated initializer is initWithNibName:bundle: (ugh!)
- (instancetype)initWithNibName:(NSString *)name bundle:(NSBundle *)bundle
{
	self = [super initWithNibName:name bundle:bundle];
	[self setup];
	return self;
}			  

outlets get set
viewDidLoad
(when geometry is determined)
 viewWillLayoutSubviews: and viewDidLayoutSubviews:
(next group can happen repeatedly as your MVC appears and disappears from the screen ...) 
 viewWillAppear: and viewDidAppear:
	(whenever geometry changes again while visible, e.g. device rotation) 
	 viewWillLayoutSubviews: and viewDidLayoutSubviews:
	(if it is autorotation, then you also get will/didRotateTo/From messages--rare to use these)
viewWillDisappear: and viewDidDisappear: 
(possibly if memory gets low ...)
 didReceiveMemoryWarning
(there is no “unload” anymore, so that’s all there is)

After instantiation and outlet-setting

-(void)viewDidLoad 
{
	[super viewDidLoad];
}

// only called once in you life of View Controller // be carefully cause the geometry(i.e bounds) of your view not yet set // so do not initialize things that geometry-dependent here

##Just before the view appear or disappear on screen, you notified

-(void)viewWillAppear:(Bool)animated
{

}

// don't put something initialize once in this method // when you viewController disappear off screen then disappear again,you better sync you model in this funcion // notice than when someone rotate the phone, you can't notice in this funcion

-(void)viewWillDisappear:(Bool)animated
{

}

##Geometry Changed

- (void)viewWillLayoutSubviews
{

}
Called to notify the view controller that its view is about to layout its subviews.


- (void)viewDidLayoutSubviews
{

}
Called to notify the view controller that its view has just laid out its subviews.

// when you geometry just changed from portrait to landscape, then ios will try to do autolayout to fit. // before that, you get viewWillLayoutSubViews then viewDieLayoutSubviews after finished.

AutoRotation Notice:

view Controller return YES from shouldAutorotate
view Controller return the new orientation in supportedInterfaceOrientations
application allow rotation to that orientation (defined in Info.plist)

##In-low Memory Situations

didReciveMemoryWarning

//maybe it's not you app resposibility, cause the message send to all app when system memory is not enough //but when you are big memory, the ios system would kill you app (cause it has the right to do it, Baby)

Clone this wiki locally