通过Window-based Application创建项目(二)
操作过程:
(1)创建Window-based Application项目;
(2)新建UIViewController类文件(带有XIB);
(3)在新建的UIViewController类的XIB上添加一个按钮;
(4)修改主文件类文件(.h 和 .m)
(5)编辑主文件类的XIB文件(增加一个UIViewController控件,同时属性nib和Class指向上面新建的UIViewController类);
第1步:创建Window-based Application项目;
这里示例项目取名为 WinBasedApp2
第2步:新建UIViewController类文件(带有XIB);
鼠标选中项目 WinBasedApp2
显示下图窗口:
注意:选中 With XIB for user interface
这里示例取名为 FirstViewController
将在Classes目录中新出现的文件 FirstViewController.xib
第3步: FirstViewController.xib
打开 FirstViewController.xib 文件并在界面上增加一个按钮,按钮标题为 first view.
第4步:修改主文件类文件
WinBasedApp2AppDelegate.h
#import <UIKit/UIKit.h>
@class FirstViewController;
@interface WinBasedApp2AppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
FirstViewController *viewController;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet FirstViewController *viewController;
@end
WinBasedApp2AppDelegate.h
#import <UIKit/UIKit.h>
@class FirstViewController;
@interface WinBasedApp2AppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
FirstViewController *viewController;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet FirstViewController *viewController;
@end
WinBasedApp2AppDelegate.m
#import "WinBasedApp2AppDelegate.h"
#import "FirstViewController.h"
@implementation WinBasedApp2AppDelegate
@synthesize window;
@synthesize viewController;
#pragma mark -
#pragma mark Application lifecycle
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
[self.window addSubview:viewController.view];
[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, called instead of applicationWillTerminate: when the user quits.
*/
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
/*
Called as part of 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.
See also applicationDidEnterBackground:.
*/
}
#pragma mark -
#pragma mark Memory management
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
/*
Free up as much memory as possible by purging cached data objects that can be recreated (or reloaded from disk) later.
*/
}
- (void)dealloc {
[viewController release];
[window release];
[super dealloc];
}
@end
#import "WinBasedApp2AppDelegate.h"
#import "FirstViewController.h"
@implementation WinBasedApp2AppDelegate
@synthesize window;
@synthesize viewController;
#pragma mark -
#pragma mark Application lifecycle
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
[self.window addSubview:viewController.view];
[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, called instead of applicationWillTerminate: when the user quits.
*/
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
/*
Called as part of 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.
See also applicationDidEnterBackground:.
*/
}
#pragma mark -
#pragma mark Memory management
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
/*
Free up as much memory as possible by purging cached data objects that can be recreated (or reloaded from disk) later.
*/
}
- (void)dealloc {
[viewController release];
[window release];
[super dealloc];
}
@end
第5步:编辑主文件类的XIB文件( MainWindow.xib )。
增加一个UIViewController控件,同时属性nib和Class指向上面新建的UIViewController类。
打开 MainWindow.xib ,在Interface Builder中的Library中找到控件ViewController,然后将该控件拖到MainWindow.xib
打开Inspector (Interface Builder > Tools > Inspector),选中刚加的ViewController控件,然后在Inspector的Identity窗口中,设置Class属性为 FirstViewController
切换到Inspector的Attributes窗口,设置NIB Name属性为 FirstViewController
备注:以上操作的时候,注意保存。
运行后,显示结果如下图: