iOS开发 从一个空项目创建ViewController和xib

1、新建一个Empty Application,只选Use Automatic Reference Counting,Use Core Data和Include Unit Tests不选。
2、Command+N 新建文件,选Cocoa Touch UIViewController subclass,SubClass of UIViewController,勾选with XIB for user interface(当然你也可以再建)。我这输的名字是MainViewController,最终生成MainViewController.h MainViewController.m MainViewController.xib三个文件。
3、打开(前缀)AppDelegate.h,代码如下:


#import 
#import "MainViewController.h"
@interface BIDAppDelegate : UIResponder 

@property (strong, nonatomic) UIWindow *window;
@property (strong,nonatomic) MainViewController * controller;

@end

AppDelegate.m,修改(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 方法:


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.controller= [[MainViewController alloc] initWithNibName:@"MainViewController" bundle:nil];
        
    UIView *mainView=self.controller.view;
    CGRect mainViewFrame=mainView.frame;
    mainViewFrame.origin.y+=[UIApplication sharedApplication].statusBarFrame.size.height;
    //将y座标移到状态栏下
    mainView.frame=mainViewFrame;
    [self.window addSubview:mainView];
    //载入
    
    
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

4、随便在MainViewController.xib中添加个控件,运行测试。

© 2012, 冰冻鱼. 请尊重作者劳动成果,复制转载保留本站链接! 应用开发笔记