`
king_tt
  • 浏览: 2108890 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

【移动开发】应用程序委托_UIApplicationDelegate

 
阅读更多

Cocoa Touch广泛地使用了委托,委托是负责为其他对象处理特定任务的对象。通过应用程序委托,能够在某些预定义时间点为UIApplication类做一些工作。

每个iOS应用程序有且仅有一个UIApplication实例,它负责应用程序的运行循环,以及处理应用程序级的功能。

在应用程序执行过程中的某些特定时间点,UIApplication将会调用特定的委托方法。例如,如果需要在程序退出时触发一段代码,可以在应用程序委托中实现applicationWillTerminate:方法。

查看BIDAppDelegate.h,查看应用程序委托的头文件。代码如下所示:

#import <UIKit/UIKit.h>

@class BIDViewController;

@interface BIDAppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@property (strong, nonatomic) BIDViewController *viewController;

@end
其中有一行代码需要特别注意:
@interface BIDAppDelegate : UIResponder <UIApplicationDelegate>
尖括号里面的内容,表示这个类遵循UIApplicationDelegate协议。


UIApplicationDelegate

协议简介

The UIApplicationDelegate protocol declares methods that are implemented by the delegate of the singleton UIApplication object. These methods provide you with information about key events in an application’s execution such as when it finished launching, when it is about to be terminated, when memory is low, and when important changes occur. Implementing these methods gives you a chance to respond to these system events and respond appropriately.

可选方法

Monitoring App State Changes
– application:willFinishLaunchingWithOptions:
– application:didFinishLaunchingWithOptions:
– applicationDidBecomeActive:
– applicationWillResignActive:
– applicationDidEnterBackground:
– applicationWillEnterForeground:
– applicationWillTerminate:
– applicationDidFinishLaunching:
Managing App State Restoration
– application:shouldSaveApplicationState:
– application:shouldRestoreApplicationState:
– application:viewControllerWithRestorationIdentifierPath:coder:
– application:willEncodeRestorableStateWithCoder:
– application:didDecodeRestorableStateWithCoder:
Providing a Window for Storyboarding
   window  property
Managing the Default Interface Orientations
– application:supportedInterfaceOrientationsForWindow:
Opening a URL Resource
– application:openURL:sourceApplication:annotation:
– application:handleOpenURL:
Managing Status Bar Changes
– application:willChangeStatusBarOrientation:duration:
– application:didChangeStatusBarOrientation:
– application:willChangeStatusBarFrame:
– application:didChangeStatusBarFrame:
Responding to System Notifications
– applicationDidReceiveMemoryWarning:
– applicationSignificantTimeChange:
Handling Remote Notifications
– application:didReceiveRemoteNotification:
– application:didRegisterForRemoteNotificationsWithDeviceToken:
– application:didFailToRegisterForRemoteNotificationsWithError:
Handling Local Notifications
– application:didReceiveLocalNotification:
Responding to Content Protection Changes
– applicationProtectedDataWillBecomeUnavailable:
– applicationProtectedDataDidBecomeAvailable:

再查看BIDApplicationDelegate.m,应用程序委托的实现。代码如下所示:

#import "BIDAppDelegate.h"

#import "BIDViewController.h"

@implementation BIDAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.viewController = [[BIDViewController alloc] initWithNibName:@"BIDViewController" bundle:nil];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    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.
}

- (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.
}

- (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.
}

- (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.
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
可以看到,应用程序委托实现了文档中列出的一些协议方法。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.viewController = [[BIDViewController alloc] initWithNibName:@"BIDViewController" bundle:nil];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}
首先创建一个窗口,然后通过夹在nib文件创建一个控制器类的实例。之后就把控制器的视图作为子视图添加到应用程序窗口中,这样视图就可以显示出来了。我们设计的视图就是以这种方式显示给用户的。


官方指南

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplicationDelegate_Protocol/


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics