I'm practicing on how TabViewcontroller works. Now I have 2 subclasses of UIViewcontroller. One is HypnosisViewController , the other is TimeViewController. What I wanted to check is how -(void)viewDidLoad works when IOS simulator gets memory warning. And I did
- Built and ran the app
- The console said "HypnosisViewcontroller loaded its view."
- Switched the other tab (TimeViewController)
- Saw the message in the console. It says "TabViewcontroller loaded its view"
- Did the simulator memory warning command in IOS simulator
- The console said "HypnoTime Received memory warning."
- Switched back to the HypnosisViewcontroller to see whether the console says "HypnosisViewcontroller loaded its view." again.
So the problem here is HypnosisViewcontroller is not destroyed and created again. (Because I can't see the log message when I switch back to HypnosisViewcontroller.)However I leaned the view not on the screen should be destroyed during the memory warning.
Did I miss something? Thanks in advance!
HypnosisViewController.m:
#import "HypnosisViewController.h"
#import "HypnosisView.h"
@implementation HypnosisViewController
-(void)loadView
{
//Create a view
CGRect frame = [[UIScreen mainScreen] bounds];
HypnosisView *v = [[HypnosisView alloc] initWithFrame:frame];
// Set it as *the* view of this view controller
[self setView:v];
}
-(id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)bundle
{
self = [super initWithNibName:nil
bundle:nil];
if(self){
//Get the tab bar item
UITabBarItem *tbi = [self tabBarItem];
//Give it a label
[tbi setTitle:@"Hypnosis"];
//Create a UIImage from a file
//This will use [email protected] on retina display devices
UIImage *i = [UIImage imageNamed:@"Hypno.png"];
// Put that image on the tab bar item
[tbi setImage:i];
}
return self;
}
-(void)viewDidLoad
{
// Always call the super implmetaion of viewDidload
[super viewDidLoad];
NSLog(@"HypnosisViewcontroller loaded its view");
}
@end
TimeViewController.m:
#import "TimeViewController.h"
@implementation TimeViewController
-(IBAction)showCurrentTime:(id)sender
{
NSDate *now = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setTimeStyle:NSDateFormatterMediumStyle];
[timeLabel setText:[formatter stringFromDate:now]];
[timeLabel2 setText:[formatter stringFromDate:now]];
}
-(id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)bundle
{
// Call the superclass's designated initializer
self = [super initWithNibName:nil
bundle:nil];
//Get a pointer to the application bundle object
// NSBundle *appBundle = [NSBundle mainBundle];
// self = [super initWithNibName:@"TimeViewController"
//bundle:appBundle];
if(self){
//Get the tab bar item
UITabBarItem *tbi = [self tabBarItem];
//Give it a label
[tbi setTitle:@"Time"];
//Create a UIImage from a file
//This will use [email protected] on retina display devices
UIImage *i = [UIImage imageNamed:@"Time.png"];
// Put that image on the tab bar item
[tbi setImage:i];
}
return self;
}
-(void)viewDidLoad
{
// Always call the super implmetaion of viewDidload
[super viewDidLoad];
NSLog(@"TimeViewcontroller loaded its view");
// [[self view] setBackgroundColor:[UIColor greenColor]];
}
@end
It is working properly. And
HypnosisViewcontroller
was destroyed and created again, becauseviewDidLoad
will be called only when all the views are initiated. So here you see the log message again when you switch back toHypnosisViewcontroller
which represent thatHypnosisViewcontroller
has been purged from memory and initiated again. You can try switch between these two view controllers without simulating memory warning, and you will only see the log message once.