0

我正在练习 TabViewcontroller 的工作原理。现在我有 UIViewcontroller 的 2 个子类。一个是 HypnosisViewController ,另一个是 TimeViewController。我想检查的是当 IOS 模拟器收到内存警告时-(void)viewDidLoad是如何工作的。我做到了

  1. 构建并运行应用程序
  2. 控制台说“HypnosisViewcontroller 加载了它的视图”。
  3. 切换了另一个选项卡(TimeViewController)
  4. 在控制台中看到了消息。它说“TabViewcontroller 加载了它的视图”
  5. IOS模拟器里有没有模拟器内存​​警告命令
  6. 控制台显示“HypnoTime 收到内存警告”。
  7. 切换回 HypnosisViewcontroller 以查看控制台是否显示“HypnosisViewcontroller 已加载其视图”。再次。

所以这里的问题是 HypnosisViewcontroller 没有被销毁并重新创建。(因为当我切换回 HypnosisViewcontroller 时我看不到日志消息。)但是我倾向于不在屏幕上的视图应该在内存警告期间被破坏。

我错过了什么?提前致谢!

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

时间视图控制器.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

在此处输入图像描述在此处输入图像描述

4

2 回答 2

1

内存警告不再导致控制器销毁/卸载他们的视图。

于 2013-12-11T09:20:03.500 回答
0

它工作正常。并且HypnosisViewcontroller被销毁并再次创建,因为viewDidLoad只有在所有视图启动时才会调用。因此,当您切换回已从内存中清除并再次启动HypnosisViewcontroller的表示时,您会再次看到日志消息。HypnosisViewcontroller您可以尝试在这两个视图控制器之间切换而不模拟内存警告,并且您只会看到一次日志消息。

于 2013-12-11T08:07:12.933 回答