生命周期

ViewController的生命周期

一、代码

- (instancetype)initWithCoder:(NSCoder *)aDecoder{
    self = [super initWithCoder:aDecoder];
    if (self) {
        NSLog(@"initWithCoder");
    }
    return self;
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        NSLog(@"initWithNibName");
    }
    return self;
}

- (void)loadView{  [super loadView];  NSLog(@"loadView");}

- (void)viewDidLoad { [super viewDidLoad];   NSLog(@"viewDidLoad");}

- (void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated];  NSLog(@"viewWillAppear");}

- (void)viewDidAppear:(BOOL)animated{ [super viewDidAppear:animated]; NSLog(@"viewDidAppear");}

- (void)viewWillDisappear:(BOOL)animated{ NSLog(@"viewWillDisappear"); }

- (void)viewDidDisappear:(BOOL)animated{ NSLog(@"viewDidDisappear"); }

- (void)viewWillLayoutSubviews{ NSLog(@"viewWillLayoutSubviews"); }

- (void)viewDidLayoutSubviews{ NSLog(@"viewDidLayoutSubviews"); }

二、打印输出

视图出现时
2016-02-26 12:53:20.546 SouFun[5193:645902] initWithNibName
2016-02-26 12:53:20.547 SouFun[5193:645902] awakeFromNib
2016-02-26 12:53:20.547 SouFun[5193:645902] loadView
2016-02-26 12:53:20.548 SouFun[5193:645902] viewDidLoad
2016-02-26 12:53:20.548 SouFun[5193:645902] viewWillAppear
2016-02-26 12:53:20.552 SouFun[5193:645902] viewWillLayoutSubviews
2016-02-26 12:53:20.552 SouFun[5193:645902] viewDidLayoutSubviews
2016-02-26 12:53:21.071 SouFun[5193:645902] viewDidAppear
视图消失时
2016-02-26 12:50:22.935 SouFun[5057:400075] viewWillDisappear
2016-02-26 12:50:22.949 SouFun[5057:400075] viewDidDisappear

三、总结

视图出现时

initWithNibName(initWithCoder)
awakeFromNib
loadView
viewDidLoad
viewWillAppear
viewWillLayoutSubviews
viewDidLayoutSubviews
viewDidAppear

视图消失时

viewWillDisappear
viewDidDisappear

APP启动顺序
//告诉代理进程启动但还没进入状态保存
- (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions

//告诉代理启动基本完成程序准备开始运行
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

//当程序载入后执行
- (void)applicationDidFinishLaunching:(UIApplication*)application

//当应用程序将要入非活动状态执行,在此期间,应用程序不接收消息或事件,比如来电话了
- (void)applicationWillResignActive:(UIApplication *)application


//当应用程序入活动状态执行,这个刚好跟上面那个方法相反
- (void)applicationDidBecomeActive:(UIApplication *)application

//当程序从后台将要重新回到前台时候调用,这个刚好跟上面的那个方法相反。
- (void)applicationWillEnterForeground:(UIApplication *)application

//当程序被推送到后台的时候调用。所以要设置后台继续运行,则在这个函数里面设置即可
- (void)applicationDidEnterBackground:(UIApplication *)application

//当程序将要退出是被调用,通常是用来保存数据和一些退出前的清理工作。这个需要要设置UIApplicationExitsOnSuspend的键值。
- (void)applicationWillTerminate:(UIApplication *)application

补充:

  • 从A控制器push到B控制器:

2016-04-20 16:29:56.019 iOS7Sampler[5940:2338171] B initWithNibName
2016-04-20 16:29:56.078 iOS7Sampler[5940:2338171] A viewWillDisappear
2016-04-20 16:29:56.078 iOS7Sampler[5940:2338171] B awakeFromNib
2016-04-20 16:29:56.078 iOS7Sampler[5940:2338171] B loadView
2016-04-20 16:29:56.078 iOS7Sampler[5940:2338171] B viewDidLoad
2016-04-20 16:29:56.103 iOS7Sampler[5940:2338171] B viewWillAppear

2016-04-20 16:29:56.129 iOS7Sampler[5940:2338171] B viewWillLayoutSubviews
2016-04-20 16:29:56.129 iOS7Sampler[5940:2338171] B viewDidLayoutSubviews
2016-04-20 16:29:56.129 iOS7Sampler[5940:2338171] A viewWillLayoutSubviews
2016-04-20 16:29:56.130 iOS7Sampler[5940:2338171] A viewDidLayoutSubviews
2016-04-20 16:29:56.633 iOS7Sampler[5940:2338171] A viewDidDisappear
2016-04-20 16:29:56.633 iOS7Sampler[5940:2338171] B viewDidAppear

  • 从B控制器pop回A控制器:

2016-04-20 16:36:57.317 iOS7Sampler[5940:2338171] B viewWillDisappear
2016-04-20 16:36:57.317 iOS7Sampler[5940:2338171] A viewWillAppear
2016-04-20 16:36:57.318 iOS7Sampler[5940:2338171] A viewWillLayoutSubviews
2016-04-20 16:36:57.319 iOS7Sampler[5940:2338171] A viewDidLayoutSubviews
2016-04-20 16:36:57.835 iOS7Sampler[5940:2338171] B viewDidDisappear
2016-04-20 16:36:57.835 iOS7Sampler[5940:2338171] A viewDidAppear
2016-04-20 16:36:57.836 iOS7Sampler[5940:2338171] A viewWillLayoutSubviews
2016-04-20 16:36:57.836 iOS7Sampler[5940:2338171] A viewDidLayoutSubviews

你可能感兴趣的:(生命周期)