该例介绍如何用代码这创建iPhone的app的window,这样可以使我们的app不依赖任何nib文件,完全邮代码实现。
1.创建基于"window-based application"的新项目"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.修改CodingWindowDemoAppDelegate.h后代码如下:
#import <UIKit/UIKit.h>
@interface CodingWindowDemoAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
}
//@property (nonatomic, retain) IBOutlet UIWindow *window;
@end
6.修改CodingWindowDemoAppDelegate.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