#import "DiscoverViewController.h"面試
@interface DiscoverViewController ()緩存
@end app
@implementation DiscoverViewControlleride
//視圖控制器view的生命週期的方法都是自動調用的ui
//注意:重寫view生命週期的相關方法必須使用superthis
//先調用父類相關的方法,spa
//面試的時候會問到view的生命週期;--只調用一次.net
//開始加載view的時候調用這個方法(可是尚未建立view)3d
- (void) loadView{rest
[super loadView];
NSLog(@"正在加載%s",__func__);
NSLog(@"想在view屬性剛開始建立可是沒有建立完成的時候作的事情,寫在這兒");
}
//view已經建立完成(包括設置好view相關的屬性)後會自動調用這個方法;
//只會被調用一次
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"加載完成");
NSLog(@"通常在這兒作視圖控制器添加子視圖,作當前視圖控制器相關的其餘設置和一些其餘屬性初始化");
//設置背景顏色
self.view.backgroundColor = [UIColor lightGrayColor];
}
//view將要顯示(尚未顯示)的時候會調用這個方法
//這個方法可能被調用屢次
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
NSLog(@"%s",__func__);
}
//view已經顯示在界面的時候--也有可能調用屢次
- (void) viewDidAppear:(BOOL)animated{
[super viewDidDisappear:animated];
NSLog(@"%s",__func__);
}
//view將要消失的時候會調用這個--也有可能調用屢次
- (void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
NSLog(@"%s",__func__);
}
//view已經消失的時候會調用這個方法--也有可能調用屢次
- (void)viewDidDisappear:(BOOL)animated{
[super viewDidDisappear:animated];
NSLog(@"%s",__func__);
}
//接收到內存警告的時候調用
//面試的時候,
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
//通常在這裏釋放沒必要要的內存空間
//清除緩存包括本地和程序的
NSLog(@"%s",__func__);
}
@end
在appdelete.m中設置根視圖控制器
#import "AppDelegate.h"
#import "DiscoverViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
_window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
[_window setBackgroundColor:[UIColor whiteColor]];
//======================================
//視圖控制器view的生命週期:視圖控制器的view屬性建立到銷燬的過程
//建立一個視圖控制器,若是不去使用這個視圖控制器的view
//那麼視圖控制器不會去建立它的view屬性
//1.建立發現類的對象
DiscoverViewController *discover = [[DiscoverViewController
alloc]init];
//2.將視圖控制器做爲window的根視圖控制器
self.window.rootViewController = discover;
[_window makeKeyAndVisible];
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
- (void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
@end