1, ViewBased Application的做法
新增类YourViewController
在classes里Add -> New File -> Cocoa Touch Classes -> UIViewController subclass -> Next -> Rename as YourViewController -> Finish
新增nib文件YourViewController.xib,SDK3.0不需要关注,上一步会自动添加
在Resources里Add -> New File -> User Interfaces -> View XIB -> Rename as YourViewController -> Finish
使用IB打开,File's Owner在Identity中Class选择YourViewController
给File's Owner对象建立一个outlet关联到View对象上
要修改的文件
YourAppDelegate.h
#import <UIKit/UIKit.h> @class YourViewController; //声明 @interface YourAppDelegate : NSObject <UIApplicationDelegate> { UIWindow *window; YourViewController *viewController; //实例变量 } @property (nonatomic, retain) IBOutlet UIWindow *window; @property (nonatomic, retain) IBOutlet YourViewController *viewController; //属性声明,一定要加 IBOutlet @end
YourAppDelegate.m
#import "YourAppDelegate.h" #import "YourViewController.h" @implementation YourAppDelegate @synthesize window; @synthesize viewController; //属性实现 - (void)applicationDidFinishLaunching:(UIApplication *)application { [window addSubview:viewController.view]; //加为子视图 [window makeKeyAndVisible]; } - (void)dealloc { [viewController release]; //释放 [window release]; [super dealloc]; } @end
MainWindow.xib
添加一个UIViewController对象
在Attributes中NIB Name选择YourViewController
在Identity中Class选择YourViewController
给YourAppDelegate对象建立一个outlet关联到这个对象上