なかなかいい入門説明資料ーーiPhone Application Tutorial

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

https://developer.apple.com/iphone/library/documentation/iPhone/Conceptual/iPhone101/iPhone101.pdf



step by stepでアプリケーションの作成手順を説明してくれて、とてもわかりやすいです。
もっと早くこの資料の存在をしておけば、いろいろ苦労しなくで済むなのに。。。と思いました。

アプリケーションデリゲート(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のドキュメント

まとめ


  • メインルーチンはmail.mファイルのmain()関数に記述する
  • main()関数内のUIApplicationMain()関数の実行により、Info.plistファイルをロードし、NSMainNibFile キーに記述されるnibファイルをロードする。アプリケーションのメインオブジェクト(defaultはUIApplicationクラス)及びアプリケーションデリゲート( NSObject )を生成する
  • UIApplicationMain()関数もメインイベントのループも起動し、イベント処理を開始させる

UIApplicationクラスについて

The UIApplication class provides a centralized point of control and coordination for applications running on iPhone OS.

Every application must have exactly one instance of UIApplication (or a subclass of UIApplication ). When an application is launched, the UIApplicationMain function is called; among its other tasks, this function create a singleton UIApplication object. Thereafter you can access this object by invoking the sharedApplication class method.

A major role of a UIApplication object is to handle the initial routing of incoming user events. It also dispatches action messages forwarded to it by control objects (UIControl) to the appropriate target objects. In addition, the UIApplication object maintains a list of all the windows (UIWindow objects) currently open in the application, so through those it can retrieve any of the application’s UIView objects. The application object is typically assigned a delegate, an object that the application informs of significant runtime events—for example, application launch, low-memory warnings, and application termination—giving it an opportunity to respond appropriately.

Applications can cooperatively handle a resource such as an email or an image file through the openURL: method. For example, an application opening an email URL with this method may cause the mail client to launch and display the message.

UIApplication defines a delegate that must adopt the UIApplicationDelegate protocol implement one or more of the methods.

The programmatic interfaces of UIApplication and UIApplicationDelegate also allow you to manage behavior that is specific to the device. You can control application response to changes in interface orientation, temporarily suspend incoming user events, and turn proximity sensing (of the user’s face) off and on again.


サンプル

As is true for every C-based application, the initial entry point for every iPhone application is a function called main. The good news is that, when you create a new project using the iPhone templates in Xcode, you do not have to write this function yourself. The project templates include a version of this function with all the code needed to start your application.

Listing 1 shows the main function for the MoveMe application. The main function is located in that project's main.m file. Every application you create will have a main function that is almost identical to this one. This function performs two key tasks. First, it creates the application's top-level autorelease pool, whose job is to reclaim the memory for Objective-C objects that are freed using their autorelease method. Second, it calls theUIApplicationMain function to create the MoveMe application's key objects, initialize those objects, and start the event-processing loop. The application does not return from this function until it quits.

Listing 1  Using the provided main function

int main(int argc, char *argv[])

{

NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

int retVal = UIApplicationMain(argc, argv, nil, nil);

[pool release];

return retVal;

}

Help


UIApplicationMain

This function is called in the main entry point to create the application object and the application delegate and set up the event cycle.

int UIApplicationMain (
int argc,
char *argv[],
NSString *principalClassName,
NSString *delegateClassName
);

Parameters
argc

The count of arguments in argv; this usually is the corresponding parameter to main.

argv

A variable list of arguments; this usually is the corresponding parameter to main.

principalClassName

The name of the UIApplication class or subclass. If you specify nilUIApplication is assumed.

delegateClassName

The name of the class from which the application delegate is instantiated. If principalClassNamedesignates a subclass of UIApplication, you may designate the subclass as the delegate; the subclass instance receives the application-delegate messages.

Return Value

The value 0. This value is always returned when the function exits successfully. If there are internal problems, the application code calls the exit system function with an appropriate error code, thus killing the application immediately without returning from this function.

Discussion

This function instantiates the application object from the principal class and and instantiates the delegate (if any) from the given class and sets the delegate for the application. It also sets up the main event loop, including the application’s run loop, and begins processing events. This function returns only on termination.