UIApplicationDelegate类

 xCode每次新建项目,都有个带有"AppDelegate"的类,它就是UIApplication的代理 ,默认遵守了UIApplicationDelegate协议

//
//  AppDelegate.h
//  UITabBarController控制器(代码)
//
//  Created by HJiang on 14/12/30.
//  Copyright (c) 2014年 HJiang. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;


@end
//
//  AppDelegate.m
//  UITabBarController控制器(代码)
//
//  Created by HJiang on 14/12/30.
//  Copyright (c) 2014年 HJiang. All rights reserved.
//

#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

/**
 *  app加载完毕的时候调用(一般只调用一次)
 *
 */
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    NSLog(@"%s",__func__);
    // Override point for customization after application launch.
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    
    UITabBarController *tabBarVc = [[UITabBarController alloc] init];
    
    UIViewController *vc1 = [[UIViewController alloc] init];
    vc1.view.backgroundColor = [UIColor redColor];
    UITabBarItem *barItem1 = vc1.tabBarItem;
    barItem1.title = @"消息";
    [tabBarVc addChildViewController:vc1];
    
    
    UIViewController *vc2 = [[UIViewController alloc] init];
    vc2.view.backgroundColor = [UIColor greenColor];
    UITabBarItem *barItem2 = vc2.tabBarItem;
    barItem2.title = @"好友";
    [tabBarVc addChildViewController:vc2];
    
    
    UIViewController *vc3 = [[UIViewController alloc] init];
    vc3.view.backgroundColor = [UIColor blueColor];
    UITabBarItem *barItem3 = vc3.tabBarItem;
    barItem3.title = @"动态";
    [tabBarVc addChildViewController:vc3];
    
    
    self.window.rootViewController = tabBarVc;
    
    [self.window makeKeyAndVisible];
    
    return YES;
}

/**
 *  app失去焦点的时候调用(UI控件不能正常使用)
 */
- (void)applicationWillResignActive:(UIApplication *)application {
     NSLog(@"%s",__func__);
    // 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.
}

/**
 *  app进入后台的时候调用(app消失不见)
 */
- (void)applicationDidEnterBackground:(UIApplication *)application {
     NSLog(@"%s",__func__);
    // 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.
}

/**
 *  app进入前台的时候调用(app显示出来)
 */
- (void)applicationWillEnterForeground:(UIApplication *)application {
     NSLog(@"%s",__func__);
    // 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.
}

/**
 *  当app获得焦点的时候调用(这时候整个app的UI控件都能正常使用)
 */
- (void)applicationDidBecomeActive:(UIApplication *)application {
     NSLog(@"%s",__func__);
    // 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.
}

/**
 *  app关闭的时候调用(一般情况下不会调用)
 *  1.一般情况下程序进入后台都是处于"休眠"状态,没有真正的在运行,这种程序关闭是不会调用此方法的
 *  2.但是有些需要进入后台后 还需要继续运行 比如:音乐播放器 QQ 等进入后台后还需继续播放或接受消息,如果这种程序关闭后才会调用此方法
 *
 */
- (void)applicationWillTerminate:(UIApplication *)application {
     NSLog(@"%s",__func__);
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end

 程序启动:

2015-01-01 19:31:04.313 UITabBarController控制器(代码)[1845:119757] -[AppDelegate application:didFinishLaunchingWithOptions:]
2015-01-01 19:31:04.352 UITabBarController控制器(代码)[1845:119757] -[AppDelegate applicationDidBecomeActive:]

 程序进入后台:

2015-01-01 19:32:58.184 UITabBarController控制器(代码)[1845:119757] -[AppDelegate applicationWillResignActive:]
2015-01-01 19:32:58.719 UITabBarController控制器(代码)[1845:119757] -[AppDelegate applicationDidEnterBackground:]

 程序进入前台:

2015-01-01 19:33:44.090 UITabBarController控制器(代码)[1845:119757] -[AppDelegate applicationWillEnterForeground:]
2015-01-01 19:33:44.602 UITabBarController控制器(代码)[1845:119757] -[AppDelegate applicationDidBecomeActive:]

根据需要可以在各自对应方法中做相应action 例如:

/**
 *  当app获得焦点的时候调用(这时候整个app的UI控件都能正常使用)
 */
- (void)applicationDidBecomeActive:(UIApplication *)application
{
//    UITabBarController *tabbarVc = (UITabBarController *)self.window.rootViewController;
//    NSLog(@"%@", NSStringFromCGRect(tabbarVc.tabBar.frame));
    
//    for (UIView *child in tabbarVc.tabBar.subviews) {
//        if ([child isKindOfClass:[UIImageView class]]) {
//            [child removeFromSuperview];
//        }
//    }
}
原文地址:https://www.cnblogs.com/HJiang/p/4197724.html