ios-使用Xib加载或Storyboard加载控制器(视图)

在ios中,通常建议使用代码布局,由于使用代码布局,后期维护容易,拓展容易,而且能够实现动态加载不少数据,但若是你对代码布局以为繁琐,以为Xib布局或者Storyboard布局比较方便适合你,那也不是不能够使用,下面来详细介绍Xib布局和Storyboard布局,本文只介绍布局搭建,关于动做响应和事件处理不会涉及:ios

一、Xib加载:

// 假如,在App启动时,我想给window加载一个视图控制器做为根控制器,这个视图控制器所带的视图(view),使用Xib加载
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    KFViewController *kfViewCtl = [[KFViewController alloc] initWithNibName:@"KFViewController" bundle:nil];
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    self.window.rootViewController = kfViewCtl;
    [self.window makeKeyAndVisible];
    return YES;
}

自定义视图控制器类:app

//
//  KFViewController.h
//
#import <UIKit/UIKit.h>

@interface KFViewController : UIViewController

@end
//
//  KFViewController.m
//  
#import "KFViewController.h"

@interface KFViewController ()

@end

@implementation KFViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end


新建Xib文件,Xib文件的名称能够随意起名:




Xib文件建立好后,点击该Xib文件,设置以下:



也就是:布局

一、点击KFViewController.xib -> File's Owner -> 身份检查器 -> Class -> KFViewController3d

二、点击File's Owner -> 按住"Ctrl"按键不放 -> 鼠标左键按住不放,拖动到"View" -> 选中viewcode


至此,Xib布局已经所有搭建好,在View当中,你就能够按照你的所想来拖放你所想要的控件来布局了。blog


二、Storyboard加载:

// 假如,在App启动时,我想给window加载一个视图控制器做为根控制器,这个视图控制器所带的视图(view),使用Storyboard加载
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    KFViewController *viewCtl = [storyBoard instantiateViewControllerWithIdentifier:@"KFViewCtl"];
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    self.window.rootViewController = viewCtl;
    [self.window makeKeyAndVisible];
    return YES;
}


自定义视图控制器类:事件

//
//  KFViewController.h
//
#import <UIKit/UIKit.h>

@interface KFViewController : UIViewController

@end
//
//  KFViewController.m
//  
#import "KFViewController.h"

@interface KFViewController ()

@end

@implementation KFViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

新建Storyboard文件,Storyboard文件的名称能够随意起名:




Storyboard文件建立好后,点击该Storyboard文件,设置以下:
it


也就是:io

一、点击MainStoryboard.storyboard -> View Controller -> 身份检查器 -> Class -> KFViewControllerclass

二、Identity -> Storyboard ID -> 填入该控制器的惟一标识


至此,Storyboard布局已经所有搭建好,在View当中,你就能够按照你的所想来拖放你所想要的控件来布局了。