轻量级应用开发之(09)创建控制器

一 创建控制器

第1种方式 通过代码控制器

HKUIViewController.h

#import <UIKit/UIKit.h>

@interface HKUIViewController : UIViewController

@end

 

HKUIViewController.m

#import "HKUIViewController.h"

@implementation HKUIViewController

@end

 

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc]init] ;
    HKUIViewController * hkvc = [[HKUIViewController alloc]init];
    hkvc.view.backgroundColor = [UIColor redColor];
    self.window.rootViewController = hkvc;
    [self.window makeKeyAndVisible];
    
    return YES;
}

 

第2种方式: 通过storyboard加载控制器。

 1) XCode创建storyboard

  Xcode -> iOS -> User interface -> Storyboard 新建一个名为 Test.storyboard文件。

2) 设置Test.storyboard为 initial View Controller

  勾选中 is initial View Controller.

 

3)拖拽一个 View Controller到 Test.storyboard中。  

   设置他的class为 HKUIViewController。

 

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc]init] ;
   
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Test" bundle:nil];
    HKUIViewController * hkvc =  [storyboard instantiateInitialViewController];
    
    self.window.rootViewController = hkvc;
    [self.window makeKeyAndVisible];
    
    return YES;
}

 也可以通过表示加载控制器。

在 Test.storyboard中 identity设置Storyboard ID为 switch。

 

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc]init] ;
   
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Test" bundle:nil];
   // HKUIViewController * hkvc =  [storyboard instantiateInitialViewController];
    HKUIViewController *hkvc = [storyboard instantiateViewControllerWithIdentifier:@"switch"];
    
    self.window.rootViewController = hkvc;
    [self.window makeKeyAndVisible];
    
    return YES;
}

 

第三种方式:通过xib加载控制器

1)通过XCode创建 Xib

   XCode -> iOS -> User Interface -> View, 建立一个名为Test.xib的xib .

2) 设置 xib的文件所有者。

  设置 Test.xib的 File's Owner 的 class。

      设置 Test.xib的 File's Owner 的 view关联。

 

 AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc]init] ;

    HKUIViewController *hkvc = [[HKUIViewController alloc]initWithNibName:@"Test" bundle:nil ];
    
    self.window.rootViewController = hkvc;
    [self.window makeKeyAndVisible];
    
    return YES;
}

 

也可以创建控制器的同时创建一个xib

1)Xcode 创建控制器

 

 

 

 

 

知识点:

commond + n 新建一个storyboard

 

posted @ 2016-05-09 22:24  陈晓楠  阅读(212)  评论(0编辑  收藏  举报