IOS的生命周期问题

开发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的内容如下

//
//  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";
        [email protected]"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");
}

@end

SecondViewController.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

上面的代码都很清晰,目的是想建立一个启动后一个界面里面包含一个button,然后点击可以进入到第二个界面去

下面是验证的结果:

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

本文原创,转载请注明出处

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-12-28 00:52:15

IOS的生命周期问题的相关文章

IOS Application生命周期

应用程序的状态 Not running(未运行):程序没启动 Inactive(未激活):程序在前台运行,不过没有接收到事件.在没有事件处理情况下程序通常停留在这个状态 Active(激活):程序在前台运行而且接收到了事件.这也是前台的一个正常的模式 Backgroud(后台):序在后台而且能执行代码,大多数程序进入这个后台后会在在这个状态上停留一会.时间到之后会进入挂起状态(Suspended).有的程序经过特殊的请求后可以长期处于Backgroud状态 Suspended(挂起):程序在后台

图解ios程序生命周期

图解ios程序生命周期 应用程序启动后状态有Active.Inactive.Background.Suspended.Not running这5种状态,几种状态的转换见下图: 在AppDelegate中实现app状态变化时的回调函数,在 app状态发生变化时,系统会执行相应回调: - (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions

IOS ViewController生命周期

ViewController ViewController是IOS开发中MVC模式中的C,ViewController是view的controller,ViewController的职责主要包括管理内部各个view的加载显示和卸载,同时负责与其他ViewController的通信和协调. 在IOS中,有两类ViewController,一类是显示内容的,比如UIViewController.UITableViewController等,同时还可以自定义继承自UIViewController的Vi

iOS应用生命周期

作为应用程序的委托对象,AppDelegate类在应用生命周期的不同阶段会回调不同的方法.首先,让我们先了解一下iOS 应用的不同状态及它们彼此间的关系,见图1 . 图1 iOS应用状态图 下面简要介绍一下iOS 应用的5种状态. ? Not Running(非运行状态).应用没有运行或被系统终止. ? Inactive (前台非活动状态).应用正在进入前台状态,但是还不能接受事件处理. ? Active (前台活动状态).应用进入前台状态,能接受事件处理. ? Background(后台状态)

iOS视图生命周期与视图控制器关系

iOS中视图是一个应用的重要组成部分,功能的实现与其息息相关,而视图控制器控制着视图,其重要性在整个应用中不言而喻. 视图生命周期与视图控制器关系 以视图的5种状态为基础,我们来系统的了解一下视图控制器的生命周期.在视图不同的生命周期,视图控制器会回调不同的方法. viewDidLoad方法:视图控制器已被实例化,在视图被加载到内存中的时候调用该方法,这个时候视图并未出现.在该方法中通常进行的是对所控制的视图进行初始化处理. 视图可见前后会调用viewWillAppear:方法和viewDidA

iOS界面生命周期过程详解

开发过Android的人都知道,每一个Android界面就是一个Activity,而每一个Activity都会有自己的生命周期, 有一系列方法会控制Activity的生命周期,如:onCreate(),onStart(),onResume(),onDestroy()等等.在iOS中,也会有这样的流程控制.这篇博客先来讨论一个iOS应用的控制流程. 在新创建的一个程序中会有一个AppDelegate.swift文件,里面包含的一系列方法体现了iOS的控制流程.下面是系统自己生成的代码,我们来感受一

iOS程序生命周期 AppDelegate

iOS的应用程序的生命周期,还有程序是运行在前台还是后台,应用程序各个状态的变换,这些对于开发者来说都是很重要的. iOS系统的资源是有限的,应用程序在前台和在后台的状态是不一样的.在后台时,程序会受到系统的很多限制,这样可以提高电池的使用和用户体验.//开发app,我们要遵循apple公司的一些指导原则,原则如下:1.应用程序的状态状态如下:Not running  未运行  程序没启动Inactive          未激活        程序在前台运行,不过没有接收到事件.在没有事件处理

【Xamarin 开发 IOS --IOS ViewController生命周期】

ViewController ViewController是IOS开发中MVC模式中的C,ViewController是view的controller,ViewController的职责主要包括管理内部各个view的加载显示和卸载,同时负责与其他ViewController的通信和协调. 在IOS中,有两类ViewController,一类是显示内容的,比如UIViewController.UITableViewController等,同时还可以自定义继承自UIViewController的Vi

iOS开发——生命周期

为了处理好应用程序的挂起.暂停等情况下的数据保存,或对应添加所需处理,我们必须了解ios生命周期. 但是不要去背去记,做个实验就好. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{    // Override point for customization after application launch.    NSLog