从一开始——我的iOS学习之路

2018-07-11 学习程序的执行顺序和UIViewContr

2018-07-11  本文已影响3人  肠粉白粥_Hoben

参考自这篇文章这篇对前文的整合

一.程序启动执行顺序

1.程序的入口

main函数,设置AppDelegate为函数的代理。

2.程序完成加载

-[AppDelegate application:didFinishLaunchingWithOptions:]

3.创建Window窗口

4.程序被激活

-[AppDelegate applicationDidBecomeActive:]

5.点击Home键

首先,程序会取消激活状态:-[AppDelegate applicationWillResignActive:]

然后,程序进入后台:-[AppDelegate applicationDidEnterBackground:]

6.重新点击进入程序

首先,程序进入前台:-[AppDelegate applicationWillEnterForeground:]

然后,程序会被激活:-[AppDelegate applicationDidBecomeActive:]

与Android的生命周期有着异曲同工之妙,但也有些许区别,比如创建进程之后,是加载\rightarrow激活的,没有applicationWillEnterForeground

  1. 创建进程

onCreate() / applicationDidFinishLaunching

  1. 进入视野(没获取焦点)

onStart() / applicationWillEnterForeground

  1. 获取焦点(可操作)

onResume() / applicationDidBecomeActive

  1. 失去焦点(还在视野内,不可操作)

onPause() / applicationWillResignActive

  1. 离开视野

onStop() / applicationDidEnterBackground

6.杀死程序

onDestroy() / applicationWillTerminate

2.在APPDelegate类实现的文件以及对应操作

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOption{// Override point for customization after application launch.
    NSLog(@"didFinishLaunchingWithOptions");
    return YES;
}

/* 当应用程序从活动状态(active)变到非活动状态(inactive时被触发调用, 这可能发生在一些临时中断下(例如:来电话、来短信)又或者程序退出时,他会先过渡到后台然后terminate 使用这方法去暂停正在进行的任务,禁用计时器,节流OpenGL ES 帧率。在游戏中应该在这个方法里面暂停游戏。 */
- (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(@"WillResignActive");
}

 /* 使用这种方法来释放共享资源,保存用户数据,无效计时器,存储足够多的应用程序状态信息来恢复您的应用程序的当前状态,以防它终止丢失数据。 如果你的程序支持后台运行,那么当用户退出时不会调用applicationWillTerminate。 */
- (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(@"DidEnterBackground");
    }

 /* 先从后台切换到非活动状态,然后进入活动状态。 */
- (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(@"WillEnterForeground");
}

/* 重启所有的任务,不管是从非活动状态还是刚启动程序,还是后台状态。 */
- (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(@"DidBecomeActive");
    }

/* 终止,game over */
- (void)applicationWillTerminate:(UIApplication *)application { 
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    NSLog(@"WillTerminate");
}

刚启动时出现了两个:


启动

按下Home键又出现了两个:


后台

点回去又出现了两个:


回到前台

由此,可以得到以下生命周期图:

活动和非活动
生命周期图

对于每个阶段的分析:

二.UIViewController生命周期

之前在视频器播放项目中遇到过关于ViewController的问题反馈,现在再把它拿出来:

首先看看单个viewController的生命周期:

  1. loadView:加载view 会多次调用并且会使viewWillLayoutSubviewsviewDidLayoutSubviews不再执行
  2. viewDidLoadview加载完毕
  3. viewWillAppear:控制器的view将要显示
  4. viewWillLayoutSubviews:控制器的view将要布局子控件
  5. viewDidLayoutSubviews:控制器的view布局子控件完成
    这期间系统可能会多次调用viewWillLayoutSubviewsviewDidLayoutSubviews 两个方法
  6. viewDidAppear:控制器的view完全显示
  7. viewWillDisappear:控制器的view即将消失的时候
  8. viewDidDisappear:控制器的view完全消失的时候

整个控制器生命周期:loadView-> viewDidLoad -> viewWillAppear -> viewWillLayoutSubviews -> viewDidLayoutSubviews -> viewDidAppear -> viewWillDisappear -> viewDidDisappear

注意:这个方法会被调用多次,如果在此创建视图,可能会创建多个,而且这个方法中执行耗时操作依然会造成跳转卡顿的问题。

下面写个程序来试试:

//视图控制器中的视图加载完成,viewController自带的view加载完成
- (void)viewDidLoad {
    NSLog(@"One--%s", __FUNCTION__);
    [super viewDidLoad];
    
    UIButton *button = [[UIButton alloc] initWithFrame: CGRectMake(self.view.frame.size.width / 2 - 100,
                                                                   self.view.frame.size.height / 2 - 30,
                                                                   100,
                                                                   30)];
    [self.view addSubview: button];
    [button setTitle: @"Click" forState: UIControlStateNormal];
    [button setBackgroundColor: [UIColor blueColor]];
    [button addTarget: self action: @selector(buttonClicked:) forControlEvents: UIControlEventTouchUpInside];
    
    
    // Do any additional setup after loading the view.
}

- (void) buttonClicked: (UIButton *) sender {
    AnotherViewController *anotherViewController = [[AnotherViewController alloc] init];
    [self.navigationController pushViewController: anotherViewController animated: YES];
    
}
//出现内存警告  //模拟内存警告:点击模拟器->hardware-> Simulate Memory Warning
- (void)didReceiveMemoryWarning {
    NSLog(@"One--%s", __FUNCTION__);
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//视图将要出现
- (void)viewWillAppear:(BOOL)animated {
    NSLog(@"One--%s", __FUNCTION__);
    [super viewWillAppear:animated];
}

//将要布局子视图
- (void) viewWillLayoutSubviews {
    NSLog(@"One--%s", __FUNCTION__);
    [super viewWillLayoutSubviews];
}

//布局子视图完毕
- (void)viewDidLayoutSubviews {
    NSLog(@"One--%s", __FUNCTION__);
    [super viewDidLayoutSubviews];
}

//视图已经出现
- (void)viewDidAppear:(BOOL)animated {
    NSLog(@"One--%s", __FUNCTION__);
    [super viewDidAppear:animated];
}

//视图将要消失 //双击Home键,向上推出程序执行该函数
- (void)viewWillDisappear:(BOOL)animated {
    NSLog(@"One--%s", __FUNCTION__);
    [super viewWillDisappear:animated];
}

//视图已经消失
- (void)viewDidDisappear:(BOOL)animated {
    NSLog(@"One--%s", __FUNCTION__);
    [super viewDidDisappear:animated];
}

下面来演示一下各种情况各个步骤的执行顺序:

1.启动

viewDidLoad -> viewWillAppear -> viewWillLayoutSubviews -> viewDidLayoutSubviews -> viewDidAppear

2.模拟Memory Warning

点击模拟器->hardware-> Simulate Memory

3.跳转到Controller2

可以看到,两个View Controller的生命周期是很符合我们设想的逻辑的,2加载完毕后告知1准备消失,2准备出现,然后2可以进行子布局的布置,布置完毕后,1先消失,随后2出现。

2--viewDidLoad -> 1--viewWillDisappear -> 2--viewWillAppear -> 2--viewWillLayoutSubviews -> 2--viewDidLayoutSubviews -> 1--viewDidDisappear -> 2--viewDidAppear

4.Controller2出栈

出栈的过程其实就是免去了加载布局和布置子布局,因此他的逻辑顺序同样是消失的先执行,出现的后执行,如下所示:

2--viewWillDisappear -> 1--viewWillAppear -> 2--viewDidDisappear -> 1--viewDidAppear

上一篇 下一篇

猜你喜欢

热点阅读