你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> IOS的生命周期問題

IOS的生命周期問題

編輯: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;
        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);
}

@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裡面的生命周期執行沒有任何影響的

只有在界面切換的時候才發現執行了某些方法。

 

自己不放也做個測試,親身體會下各種情況,很有用的

 

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved