0

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

  1. Built and ran the app
  2. The console said "HypnosisViewcontroller loaded its view."
  3. Switched the other tab (TimeViewController)
  4. Saw the message in the console. It says "TabViewcontroller loaded its view"
  5. Did the simulator memory warning command in IOS simulator
  6. The console said "HypnoTime Received memory warning."
  7. 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 Hypno@2x.png 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 Time@2x.png 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

enter image description hereenter image description here

Toshi
  • 6,012
  • 8
  • 35
  • 58

2 Answers2

1

Memory Warnings don't cause the Controllers to destroy/unload their views anymore.

Toshi
  • 6,012
  • 8
  • 35
  • 58
0

It is working properly. And HypnosisViewcontroller was destroyed and created again, because viewDidLoad will be called only when all the views are initiated. So here you see the log message again when you switch back to HypnosisViewcontroller which represent that HypnosisViewcontroller 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.

Eric Qian
  • 2,246
  • 1
  • 18
  • 15
  • @:Eric Quian Thank you for the answer! Oh I forgot adding something important. I can't see the log message when I switch back to HypnosisViewcontroller. – Toshi Dec 11 '13 at 08:12
  • OK, so you mean when you switch back to `HypnosisViewcontroller` after you simulated a memory warning. The message isn't showing – Eric Qian Dec 11 '13 at 08:14