Xcode改成不用MainWindow.xib 和 RootViewController.xib 的方法

来源:互联网 发布:中小型企业erp软件 编辑:程序博客网 时间:2024/06/04 16:21
[适用于 Xcode 3.2.5  iOS 4.2 development platform]
或者你有洁癖, 或者你只是觉得上传svn/cvs 多了几十Kbytes觉得不爽, 或者纯为兴趣追求技术底层的奥秘
甚至是为了快一点点(真的快了, 起码debug的时候编译器不用读取xib)

很久以前, 在开始项目的时候, 按了 Xcode:  New Project -> Navigation-based Application 
这个文档会适合你, 其他类型可能有部分适用, 自己看着办

$project 是你的项目文件夹, 你可以找到:
$project/MainWindow.xib
$project/RootViewController.xib
$project/main.m
$project/[XXX]-Info.plist
同时, 在 $project/Classes 内可以找到:
RootViewController.m / .h 
[XXX]AppDelegate.m / .h   
[XXX] 代表项目名称, 一般只有一个 AppDelegate, 所以很好找。

-----------------------------------------------------------------------------------------------------------------------------
1. 首先, 修改 $project/main.m :  (Xcode: 在 Other Sources 内)
int main(int argc, char *argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    // 这是原来的语句, 先保留
    // int retVal = UIApplicationMain(argc, argv, nil, nil);
    // peter: remove MainWindow.xib 需要修改成以下 [XXX] 对应 $project/Classes 内的 [XXX]AppDelegate.m/.h
    int retVal = UIApplicationMain(argc, argv, @"UIApplication", @"[XXX]AppDelegate");
    [pool release];
    return retVal;
}

-----------------------------------------------------------------------------------------------------------------------------
2. 测试, 把 MainWindow.xib 改名(先别删除)
2.1. 在 Xcode: Resources 内, 右键点 MainWindow.xib, 按 Delete : Delete Reference (这样不会删除文档)

note: peter found that 2.2. 需要做完 3. 才做这样会才可以测试 2.4
2.2. 在 Xcode: Resources 内, 点 [XXX]-Info.plist,  右边的窗口显示 plist 的内容, 一般在最后有一栏
        Key Value
        -----------------------------------------------------------------------
        Main nib file base name MainWindow  
        把这个删除(右键, cut)
        或者(高级用户才看, 这个已经完成):
        用 vi 打开 [XXX]-Info.plist, 找到 
        <key>NSMainNibFile</key> 
        <string>MainWindow</string> 
       把这个删除也行
2.3. 使用 Finder, 在 $project 内找到 MainWindow.xib,   改名为  MainWindow.xxx  
       xxx 是随意, 作为备份用, 万一出问题,可以回头

2.4.  回到 Xcode, Clean 一次, 在 Build and Run, 如果成功, 可以直接把 MainWindow.xxx 删除
-----------------------------------------------------------------------------------------------------------------------------

3. 修改  [XXX]AppDelegate.m / .h

3.1. 修改 [XXX]AppDelegate.h
#import <UIKit/UIKit.h>
#import "RootViewController.h"  // 加入这个

@interface FormDesignerAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
    UINavigationController *navigationController;
    RootViewController * rootViewController;  // 加入这个
}

// 删除(注释) 以下两行
//@property (nonatomic, retain) IBOutlet UIWindow *window;
//@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;

3.2.  修改 [XXX]AppDelegate.m 
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    
    // order is important  注意顺序不要弄错
    window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    rootViewController = [[RootViewController alloc] init];
    navigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
    // 另外一种写法是 
    // navigationController = [[UINavigationController alloc] init];
    // [navigationController pushViewController:rootViewController animated:NO];  
    
    // 以下两行是本身有的, 必须在上面初始化以后进行 (代替了 IBout
    // Add the navigation controller's view to the window and display.
    [self.window addSubview:navigationController.view];
    [self.window makeKeyAndVisible];

    return YES;
}

3.3. 测试: 如果ok, 可以把 MainWindow.xib 删除, 以及 [XXX]-Info.plist 内的 MainWindow 删除 
   (参考2.2.)
-----------------------------------------------------------------------------------------------------------------------------

Bonus:  如何删除 RootViewController.xib 
- 这个很简单, 如果你没有经过Interface Builder 来设计 table 内容, 那么你可以
  直接把 RootViewController.xib 删除 (首先在 Xcode 删除, 再利用 Finder 删除, 最好备份)

如果你适用 Interface Builder 来设计 table 内容, 那么你需要先把内容用程序的方法来实现,
修改 RootViewController.m 就可以.
例如, 有三个cell, 就是需要实现以下两个最基础的函数, 作为显示。
选择后的动作请参考: didSelectRowAtIndexPath

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
     return 3;
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    
    // Configure the cell.
    switch (indexPath.row) {
        case 0:   cell.textLabel.text = @"one"; break;
        case 1:   cell.textLabel.text = @"two"; break;
        case 2:   cell.textLabel.text = @"three"; break;
        default:   cell.textLabel.text = @"unknown"; break;
    }
    return cell;
}

note from the Class reference of UITableViewController:
if no nib file is specified or if the nib file defines no data source or delegate,
UITableViewController sets the data source and the delegate of the table view to self.

Reference:

http://alexeckermann.com/post/2711036222/quick-tip-going-completely-xib-less

转自:http://wapeter.blog.163.com/blog/static/12883050420111313631915/

原创粉丝点击