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 @   申龙斌的程序人生  阅读(4552)  评论(2编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示