TabViewcontroller の仕組みについて練習しています。これで、UIViewcontroller の 2 つのサブクラスができました。1 つは HypnosisViewController で、もう 1 つは TimeViewController です。私が確認したかったのは、IOSシミュレーターがメモリ警告を受け取ったときに-(void)viewDidLoadがどのように機能するかです。そして、私はしました
- アプリのビルドと実行
- コンソールには、「HypnosisViewcontroller がそのビューをロードしました」と表示されました。
- 別のタブ(TimeViewController)に切り替えました
- コンソールでメッセージを見ました。「TabViewcontrollerがそのビューをロードしました」と表示されます
- IOSシミュレーターでシミュレーターメモリ警告コマンドを実行しました
- コンソールに「HypnoTime Received memory warning」と表示されました。
- 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
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