iPhone开发笔记[15/50]:指定启动的主界面XIB文件,在xcode4.2里可以不指定MainWindow.xib了

在项目的Target的Summary的iPhone/iPod Deployment Info的Main Interface可以选择主程序启动时要加载的XIB文件。

在xcode4.2里面新建的Empty Window模板里不再出现MainWindow.xib了,看出代码更简洁了,我从4.2中把这些代码复制过来,稍微修改,就可以在4.0上编译了,这个工程可以 点这里下载

修改了下面几个地方:

(1)把Deployment Target从iOS 5.0改回到iOS4.3。

(2)把strong 改回retain。

Objective-C Automated Reference Counting (ARC)里面,strong用来代替retain。

(3)在LLVM3.0里又增加了@autoreleasepool这个特性,代码更简洁,但旧编译器不认了,改回原来的NSAutoreleasePool 代码即可。

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

(4)main.m里调用UIApplicationMain时,第4个参数把AppDelegate的类填上即可。

int retVal = UIApplicationMain(argc, argv, nil, NSStringFromClass([YourAppDelegate class]));

现在想执行主程序后,启动一个带导航栏的表视图,就可以这样了。

要在YourAppDelegate.h里加上navController

#import <UIKit/UIKit.h>

@interface YourAppDelegate : UIResponder <UIApplicationDelegate>

@property (retain, nonatomic) UIWindow *window;

//主窗口上要出现导航栏
@property (retain, nonatomic) UINavigationController *navController;

@end

在YourAppDelegate.m里:

@synthesize navController;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// 下面的MyTableViewController是一个UITableViewController
MyTableViewController *myTableViewController=[[[MyTableViewController alloc]initWithStyle:UITableViewStylePlain]autorelease];
self.navController = [[UINavigationController alloc] initWithRootViewController:myTableViewController];
[self.window addSubview:navController.view];


self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}

在YourAppDelegate.m里的其它代码都不用改。

这样虽然没有MainWindow.xib,也可以用代码启动一个带导航栏的表视图了。

posted @ 2011-11-10 08:25  申龙斌的程序人生  阅读(4544)  评论(2编辑  收藏  举报