我正在练习TabViewcontroller的工作方式。现在,我有2个UIViewcontroller的子类。
一个是HypnosisViewController,另一个是TimeViewController。
我想检查的是当IOS模拟器收到内存警告时-(void)viewDidLoad的工作方式。
我做到了
生成并运行该应用
控制台说“ HypnosisViewcontroller加载了它的视图。”
切换另一个选项卡(TimeViewController)
在控制台中看到该消息。它说“ TabViewcontroller加载了它的视图”
在iOS模拟器中是否做了模拟器内存警告命令
控制台显示“ HypnoTime收到内存警告”。
切换回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
最佳答案
内存警告不再导致控制器破坏/卸载其视图。