标签:ios生命周期 uiviewcontroller生命周期
开发Android必须得清楚Android生命周期才能很好的掌控程序的框架,让整个项目思路更加清晰流畅,因此IOS也是必须要了解IOS的生命周期
先从一个简单的实例来看看
AppDelegate.m文件里面的内容如下:
// // AppDelegate.m // SwitchView // // Created by Pishum on 15/5/5. // Copyright (c) 2015年 Pishum. All rights reserved. // #import "AppDelegate.h" #import "ViewController.h" @interface AppDelegate () { // ViewController *main; // UINavigationController *nav; } @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. NSLog(@"APP didFinishLaunchingWithOptions"); // self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // // Override point for customization after application launch. // // _storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; // // main = [_storyBoard instantiateViewControllerWithIdentifier:@"main"]; // nav = [[UINavigationController alloc] initWithRootViewController: main]; // [self.window addSubview:nav.view]; 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. NSLog(@"APP applicationWillResignActive"); } - (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. NSLog(@"APP applicationDidEnterBackground"); } - (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. NSLog(@"APP applicationWillEnterForeground"); } - (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. NSLog(@"APP applicationDidBecomeActive"); } - (void)applicationWillTerminate:(UIApplication *)application { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. NSLog(@"APP applicationWillTerminate"); } @end
// // ViewController.m // SwitchView // // Created by Pishum on 15/5/5. // Copyright (c) 2015年 Pishum. All rights reserved. // #import "AppDelegate.h" #import "ViewController.h" #import "SecondViewController.h" #import "TestXXX.h" @interface ViewController () { } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; UIBarButtonItem *backbutton = [[UIBarButtonItem alloc]init]; backbutton.title = @"返回列表"; self.navigationItem.backBarButtonItem = backbutton; // Do any additional setup after loading the view, typically from a nib. NSLog(@"A viewDidLoad"); } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (IBAction)startNewView:(id)sender { UIButton* btn = (UIButton*)sender; switch (btn.tag) { case 1: // [self performSegueWithIdentifier:@"second" sender:self]; // NSLog(@"onclick ==%@",self.navigationController); break; // default: // break; } } - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([[segue identifier] isEqualToString:@"second"]) { // id object = @"传入一个值"; TestXXX *object = [[TestXXX alloc]init]; object.name = @"xxx"; object.age=@"22"; [[segue destinationViewController] setPrevData:object]; } } - (void)viewDidUnload{ NSLog(@"A viewDidUnload"); } - (void)viewWillAppear:(BOOL)animated{ NSLog(@"A viewWillAppear"); } - (void)viewDidAppear:(BOOL)animated{ NSLog(@"A viewDidAppear"); } - (void)viewWillDisappear:(BOOL)animated{ NSLog(@"A viewWillDisappear"); } - (void)viewDidDisappear:(BOOL)animated{ NSLog(@"A viewDidDisappear"); } @endSecondViewController.m的内容如下
// // SecondViewController.m // SwitchView // // Created by Pishum on 15/5/5. // Copyright (c) 2015年 Pishum. All rights reserved. // #import "SecondViewController.h" @interface SecondViewController (){ } @end @implementation SecondViewController //TestXXX *test; //NSString* str2; - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. NSLog(@"B viewDidLoad str=%@",_str); NSLog(@"B name=%@ age=%@",_test.name,_test.age); } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ - (IBAction)back:(id)sender { NSLog(@"点击了第二个btn"); [self dismissViewControllerAnimated:YES completion:nil]; } -(id)setPrevData:(id)sender{ _str = sender; _test = sender; NSLog(@"输出上个界面得到的信息====%@ %@ %@",sender,_test.name,_test.age); return sender; } -(void)setThePrevData:(TestXXX*)sender{ _test = sender; // return sender; } - (void)viewDidUnload{ NSLog(@"B viewDidUnload"); } - (void)viewWillAppear:(BOOL)animated{ NSLog(@"B viewWillAppear"); } - (void)viewDidAppear:(BOOL)animated{ NSLog(@"B viewDidAppear"); } - (void)viewWillDisappear:(BOOL)animated{ NSLog(@"B viewWillDisappear"); } - (void)viewDidDisappear:(BOOL)animated{ NSLog(@"B viewDidDisappear"); } @end
下面是验证的结果:
UIViewController生命周期
- (void)viewDidLoad; 启动
- (void)viewDidUnload;
- (void)viewWillAppear:(BOOL)animated; (很常用)
- (void)viewDidAppear:(BOOL)animated; 更新视图
- (void)viewWillDisappear:(BOOL)animated; 即将消失
- (void)viewDidDisappear:(BOOL)animated; O已经消失
启动
APP didFinishLaunchingWithOptions
A viewDidLoad
A viewWillAppear
A viewDidAppear
APP applicationDidBecomeActive
点击Home键
APP applicationWillResignActive
APP applicationDidEnterBackground
重新进来
APP applicationWillEnterForeground
APP applicationDidBecomeActive
点击按键进入B界面
B viewDidLoad
A viewWillDisappear
B viewWillAppear
B viewDidAppear
A viewDidDisappear
点击了第二个btn返回到A界面
B viewWillDisappear
A viewWillAppear
A viewDidAppear
B viewDidDisappear
在B界面双击Home然后清理程序
APP applicationWillResignActive
APP applicationDidEnterBackground
B viewWillDisappear
B viewDidDisappear
APP applicationWillTerminate
从上面得出的结果很清晰的可以看到,点击home键是对UIViewController里面的生命周期执行没有任何影响的
只有在界面切换的时候才发现执行了某些方法。
自己不放也做个测试,亲身体会下各种情况,很有用的
工程源码地址
https://github.com/pishum/SwitchView
本文原创,转载请注明出处
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:ios生命周期 uiviewcontroller生命周期
原文地址:http://blog.csdn.net/pishum/article/details/47159797