iPhoneネイティブアプリケーション開発概念2ーアプリケーションデリゲート

  • 投稿日:
  • by
  • カテゴリ:

アプリケーションデリゲート(Application Delegate)の定義
アプリケーションデリゲートはネイティブプログラムの1つ重要概念である。Interface Builderを使用してデリゲートの指定ができる。下記グラフにあるFile's Owner objectのdeligateをMoveMeAppDelegateに指定している。

One of the most important architectural details of your project is defining the application delegate object, which is instantiated from a class you provide in your project.

You can use Interface Builder to designate the class as the application delegate.

Interface Builder saves your user interface in a file known as a nib file, which is an archive of your application's object graph.

To see how the application delegate relationship is established, click the File's Owner icon in the nib file document window (titled “MainWindow.xib”), show the Inspector window (choose Tools > Inspector), and click the Inspector window's Application Connections tab. As shown in Figure 3, the Inspector shows that the File's Owner object (which represents the application in the nib file) has a delegate outlet connected to the MoveMeAppDelegate object.
appdelegate.jpg

デリゲートの役割

The application delegate object works in tandem with the standard UIApplication object to respond to changing conditions in the application. The application object does most of the heavy lifting, but the delegate is responsible for several key behaviors, including the following:

・Setting up the application's window and initial user interface
・Performing any additional initialization tasks needed for your custom data engine
・Opening content associated with the application's custom URL schemes
・Responding to changes in the orientation of the device
・Handling low-memory warnings
・Handling system requests to quit the application

At launch time, the most immediate concern for the delegate object is to set up and present the application window to the user. The delegate should also perform any tasks needed to prepare your application for immediate use, such as restoring the application to a previous state or creating any required objects. When the application quits, the delegate needs to perform an orderly shutdown of the application and save any state information needed for the next launch cycle.

サンプルコード
アプリケーションデリゲートのソースコードのイメージは下記となります。

#import "UIKit/UIKit.h"
@class iphone_helloworldViewController;
@interface iphone_helloworldAppDelegate : NSObject {
IBOutlet UIWindow *window; /* UIWindow, Interface Builderより指定される */
MyViewController *myViewController;
}
@property (nonatomic, retain) UIWindow *window;
@property (nonatomic, retain) MyViewController *myViewController;
@end

@implementation iphone_helloworldAppDelegate
@synthesize window;
/*
アプリケーションはデリゲートのインスタンスにapplicationDidFinishLaunching()メッセージを送信する。
applicationDidFinishLaunching()はUIやデータの初期化を行う。
*/
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Set up the view controller
MyViewController *aViewController = [[MyViewController alloc] initWithNibName:@"HelloWorld" bundle:[NSBundle mainBundle]];
self.myViewController = aViewController;
[aViewController release];
/* UI初期化以外に、データの初期化やアプリケーション設定などの処理もここで行う */
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackOpaque];
// Add the view controller's view as a subview of the window
UIView *controllersView = [myViewController view];
[window addSubview:controllersView]; /* WindowにUIViewを追加 */
[window makeKeyAndVisible]; /* Windowが見えるようにする */
}
- (void)dealloc {
[myViewController release];
[window release];
[super dealloc];
}
@end

参考リンク
Appleのドキュメント