2013-12-11 96 views
0

我正在练习TabViewcontroller如何工作。现在我有2个UIViewcontroller的子类。 一个是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 [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 

enter image description hereenter image description here

回答

0

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

+0

@:Eric Quian谢谢你的回答!哦,我忘了添加重要的东西。当我切换回HypnosisViewcontroller时,我看不到日志消息。 – Tosh

+0

OK,所以你的意思是当你模拟一个内存警告后,当你切换回'HypnosisViewcontroller'时。该消息没有显示 –

+0

是的,没错! – Tosh

1

内存警告不会导致控制器摧毁/卸载其视图了。