我正在练习TabViewcontroller的工作原理。现在我有2个UIViewcontroller的子类。 一个是HypnosisViewController,另一个是TimeViewController。 我想检查的是当IOS模拟器获取内存警告时 - (void)viewDidLoad 的工作原理。 我做了
所以这里的问题是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
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
答案 0 :(得分:1)
内存警告不会导致控制器再破坏/卸载他们的视图。
答案 1 :(得分:0)
工作正常。 HypnosisViewcontroller
被销毁并再次创建,因为只有在启动所有视图时才会调用viewDidLoad
。因此,当您切换回HypnosisViewcontroller
时,您会再次看到日志消息,表示HypnosisViewcontroller
已从内存中清除并再次启动。您可以尝试在这两个视图控制器之间切换而不模拟内存警告,并且您只能看到一次日志消息。