アプリのwindowをコードで初期化する
6628 ワード
この例では、iPhoneのappのwindowをコードで作成する方法を紹介します.これにより、私たちのappはnibファイルに依存せず、完全にメールコードで実現することができます.
1.「window-basedアプリケーション」に基づく新しいプロジェクト「CodingWindowDemo」を作成する.
2.MainWindowを削除します.xibファイル;
3.CodingWindowDemo-Infoを削除する.plistファイルの「Main nib file base name」行.
4.mainを修正する.mコード:
5.C o d i n g W o ndowDemoAppDelegateを修正する.h後のコードは以下の通りである.
6.C o d i n g W o ndowDemoAppDelegateを修正する.m後のコードは以下の通りです.
1.「window-basedアプリケーション」に基づく新しいプロジェクト「CodingWindowDemo」を作成する.
2.MainWindowを削除します.xibファイル;
3.CodingWindowDemo-Infoを削除する.plistファイルの「Main nib file base name」行.
4.mainを修正する.mコード:
:
int
retVal
=
UIApplicationMain(argc, argv, nil, nil);
:
int
retVal
=
UIApplicationMain(argc, argv, nil,
@"
CodingWindowDemoAppDelegate
"
);
5.C o d i n g W o ndowDemoAppDelegateを修正する.h後のコードは以下の通りである.
#import
<
UIKit
/
UIKit.h
>
@interface CodingWindowDemoAppDelegate : NSObject
<
UIApplicationDelegate
>
{
UIWindow
*
window;
}
//
@property (nonatomic, retain) IBOutlet UIWindow *window;
@end
6.C o d i n g W o ndowDemoAppDelegateを修正する.m後のコードは以下の通りです.
#import
"
CodingWindowDemoAppDelegate.h
"
@implementation CodingWindowDemoAppDelegate
//
@synthesize window;
#pragma
mark -
#pragma
mark Application lifecycle
-
(BOOL)application:(UIApplication
*
)application didFinishLaunchingWithOptions:(NSDictionary
*
)launchOptions {
//
Override point for customization after application launch.
window
=
[[UIWindow alloc] initWithFrame:CGRectMake(
0
,
0
,
320
,
480
)];
window.backgroundColor
=
[UIColor blueColor];
//
view
//
...
[window makeKeyAndVisible];
return
YES;
}
#pragma
mark -
#pragma
mark Memory management
-
(
void
)dealloc {
[window release];
[super dealloc];
}
@end