**IOS:xib檔案解析(xib和storyboard的比較,一個輕量級一個重量級)

來源:互聯網
上載者:User

標籤:

使用Xcode做iOS項目,經常會和Xib檔案打交道,因為Xib檔案直觀的展現出運行時視圖的外觀,所以上手非常容易,使用也很方便,但對於從未用純程式碼寫過視圖的童鞋,多數對Xib的理解有些片面。

Xib檔案是什嗎?

A nib file describes the visual elements of your application’s user interface, including windows, views, controls, and many others. It can also describe non-visual elements, such as the objects in your application that manage your windows and views. 

上面是Guides上的定義,nib檔案(就是Xib檔案)是描述套用面板的視覺元素,包含了視窗、視圖、控制和其他,它也可以描述非視覺元素,如你應用中管理視窗和視圖的對象。

也就是說,Xib就是一個描述文檔,這裡麵包含了使用者介面和使用者介面相關元素。這樣做的主要原因是,方便程式員,因為其是“所見即所得 (WYSIWYG)”的,程式員不需要像原來一樣寫一堆代碼,然後運行出來才能看到介面元素的執行結果,這樣是很低效的。更多內容可參看官網

Xib檔案是如何做到“所見即所得 (WYSIWYG)”?

在Xcode中,以“source code”的方式,開啟Xib檔案,發現它是一份以XML編碼的檔案,也就是說Xib檔案的本質是用一份XML的指令碼描述了程式員在Xcode中對檔案中View及其子類等的設定,這裡我們已經知道在Xcode中的設定是如何儲存下來的,接著Xib檔案如何在應用執行是重建到記憶體中。

在Xcode中建立一份空白的Xib檔案,會得到如下代碼:

這裡對XML進行瞭解析並圖形顯示,可以看到Xib檔案還是比較複雜的,前面展開的這幾項均是系統和版本描述,可以先忽略,重點是藍色框內的內容(以下解釋均非官方版本,請謹慎閱讀)。

我們可以從名稱“IBDocument.IntegratedClassDependencies”直接看出,這個數組是完成對IBDocument相關類的描述,預設會有IBProxyObject(與File‘s owner有關)、IBUIView(與主視圖有關),如果我們拖進一個label進入view,這裡便會有UILabel類的描述,如果選擇layout,會有NSLayoutConstraint。

 

這塊有Metadata,暫時還不知道是如何使用的,但接下,我們將看到View的全貌

如同RootObjects的名字,這個數組包含了所有的Xib檔案中的對象,第一個是Files Owner,第二個是FirstResponder,可以看到其都使用ProxyObject。這裡就有一個關於Xib檔案實現的細節,當我們的應用裡是誰要去讀入和使用到這份使用者介面時,想到的是我們代碼裡的ViewController,這時ViewController已經在記憶體中,那從Xib檔案讀出的這些配置如何與ViewController關聯就是一個問題了,這裡使用ProxyObject在Xib檔案中做ViewController的代理對象,如

接著看XML:

IBClassDescriber中,在我們設定關聯後,將有IBOutlet和IBAction與Xib的相互關聯描述在這裡,IBObjectContainer包含了對象之間的關係,

通過記錄的這些內容,在Xib被unarchive後,將新建立的對象(通過Xib檔案建立)的設定成相互的reference。

這應該是個複雜的過程,參看官方文檔Xib檔案載入的步驟如下:

1 載入nib檔案的內容和其引用的資源檔到記憶體:
    2 unarchives nib檔案的object graph資料並且執行個體化的這些對象。根據對象的類型和其在文檔中的編碼來初始化每個新對象。

        3 通過初始化方法,它重建的所有串連(actions, outlets, and bindings)在nib檔案中的對象。

以上3步,iOS與OS X不太相同,這裡主要說下在iOS中,也就是nib檔案中的建立的對象,重新串連每沒一個outlet指向時,是基於NSKeyValueCoding Protocol ,通過setValue:forKey: 來完成的。

以上主要是通過看Xib檔案XML的資料和官方文檔,完成了對Xib檔案中內容的大致理解,對其細節實現並沒有深入分析,主要的目的讓我們在使用Xib檔案,對它不再陌生,出現Xib檔案執行時錯誤,可以根據錯誤資訊來推測出錯的地方。

 

Nib Files Store the Objects of Your Application’s User Interface

 

iOS開發UI篇—xib的簡單使用

 

一、簡單介紹

xib和storyboard的比較,一個輕量級一個重量級。

共同點:

都用來描述軟體介面

都用Interface Builder工具來編輯

不同點:

Xib是輕量級的,用來描述局部的UI介面

Storyboard是重量級的,用來描述整個軟體的多個介面,並且能展示多個介面之間的跳轉關係

二、xib的簡單使用

1.建立xib檔案

建立的xib檔案命名為appxib.xib

2.對xib進行設定

  根據程式的需要,這裡把view調整為自由布局

建立view模型(設定長寬等參數)

調整布局和內部的控制項

 

完成後的單個view

3.使用xib檔案的程式碼範例

YYViewController.m檔案代碼如下:

 1 // 2 //  YYViewController.m 3 //  10-xib檔案的使用 4 // 5 //  Created by apple on 14-5-24. 6 //  Copyright (c) 2014年 itcase. All rights reserved. 7 // 8  9 #import "YYViewController.h"10 #import "YYapp.h"11 12 @interface YYViewController ()13 @property(nonatomic,strong)NSArray *app;14 @end15 16 @implementation YYViewController17 18 //1.載入資料資訊19 -(NSArray *)app20 {21     if (!_app) {22         NSString *path=[[NSBundle mainBundle]pathForResource:@"app.plist" ofType:nil];23         NSArray *temparray=[NSArray arrayWithContentsOfFile:path];24         25         //字典轉模型26         NSMutableArray *arrayM=[NSMutableArray array ];27         for (NSDictionary *dict in temparray) {28             [arrayM addObject:[YYapp appWithDict:dict]];29         }30         _app=arrayM;31     }32     return _app;33 }34 35 //建立介面原型36 - (void)viewDidLoad37 {38     [super viewDidLoad];39     NSLog(@"%d",self.app.count);40     41     //九宮格布局42     int totalloc=3;43     CGFloat appviewW=80;44     CGFloat appviewH=90;45     CGFloat margin=(self.view.frame.size.width-totalloc*appviewW)/(totalloc+1);46     47     int count=self.app.count;48     for (int i=0; i<count; i++) {49         50         int row=i/totalloc;51         int loc=i%totalloc;52         CGFloat appviewX=margin + (margin +appviewW)*loc;53         CGFloat appviewY=margin + (margin +appviewH)*row;54         YYapp *app=self.app[i];55         56         //拿出xib視圖57        NSArray  *apparray= [[NSBundle mainBundle]loadNibNamed:@"appxib" owner:nil options:nil];58         UIView *appview=[apparray firstObject];59         //載入視圖60         appview.frame=CGRectMake(appviewX, appviewY, appviewW, appviewH);61         62         UIImageView *appviewImg=(UIImageView *)[appview viewWithTag:1];63         appviewImg.image=app.image;64         65         UILabel *appviewlab=(UILabel *)[appview viewWithTag:2];66         appviewlab.text=app.name;67         68         UIButton *appviewbtn=(UIButton *)[appview viewWithTag:3];69         [appviewbtn addTarget:self action:@selector(appviewbtnClick:) forControlEvents:UIControlEventTouchUpInside];70         appviewbtn.tag=i;71         72         [self.view addSubview:appview];73     }74 }75 76 /**按鈕的點擊事件*/77 -(void)appviewbtnClick:(UIButton *)btn78 {79     YYapp *apps=self.app[btn.tag];80     UILabel *showlab=[[UILabel alloc]initWithFrame:CGRectMake(60, 450, 200, 20)];81     [showlab setText:[NSString stringWithFormat: @"%@下載成功",apps.name]];82     [showlab setBackgroundColor:[UIColor lightGrayColor]];83     [self.view addSubview:showlab];84     showlab.alpha=1.0;85     86     //簡單的動畫效果87     [UIView animateWithDuration:2.0 animations:^{88         showlab.alpha=0;89     } completion:^(BOOL finished) {90         [showlab removeFromSuperview];91     }];92 }93 94 @end

運行效果:

三、對xib進行連線樣本

1.連線樣本

建立一個xib對應的視圖類,繼承自Uiview

 


在xib介面右上方與建立的視圖類進行關聯

把xib和視圖類進行連線

注意:在使用中把weak改成為強引用。否則...

2.連線後的程式碼範例

YYViewController.m檔案代碼如下:

 1 // 2 //  YYViewController.m 3 //  10-xib檔案的使用 4 // 5 //  Created by apple on 14-5-24. 6 //  Copyright (c) 2014年 itcase. All rights reserved. 7 // 8  9 #import "YYViewController.h"10 #import "YYapp.h"11 #import "YYappview.h"12 13 @interface YYViewController ()14 @property(nonatomic,strong)NSArray *app;15 @end16 17 @implementation YYViewController18 19 //1.載入資料資訊20 -(NSArray *)app21 {22     if (!_app) {23         NSString *path=[[NSBundle mainBundle]pathForResource:@"app.plist" ofType:nil];24         NSArray *temparray=[NSArray arrayWithContentsOfFile:path];25         26         //字典轉模型27         NSMutableArray *arrayM=[NSMutableArray array ];28         for (NSDictionary *dict in temparray) {29             [arrayM addObject:[YYapp appWithDict:dict]];30         }31         _app=arrayM;32     }33     return _app;34 }35 36 //建立介面原型37 - (void)viewDidLoad38 {39     [super viewDidLoad];40     NSLog(@"%d",self.app.count);41     42     //九宮格布局43     int totalloc=3;44     CGFloat appviewW=80;45     CGFloat appviewH=90;46     CGFloat margin=(self.view.frame.size.width-totalloc*appviewW)/(totalloc+1);47     48     int count=self.app.count;49     for (int i=0; i<count; i++) {50         51         int row=i/totalloc;52         int loc=i%totalloc;53         CGFloat appviewX=margin + (margin +appviewW)*loc;54         CGFloat appviewY=margin + (margin +appviewH)*row;55         YYapp *app=self.app[i];56         57         //拿出xib視圖58        NSArray  *apparray= [[NSBundle mainBundle]loadNibNamed:@"appxib" owner:nil options:nil];59         60         //注意這裡的類型名!61         //UIView *appview=[apparray firstObject];62         YYappview  *appview=[apparray firstObject];63        64         //載入視圖65         appview.frame=CGRectMake(appviewX, appviewY, appviewW, appviewH);66           [self.view addSubview:appview];67         68         appview.appimg.image=app.image;69         appview.applab.text=app.name;70         appview.appbtn.tag=i;71         72         [ appview.appbtn addTarget:self action:@selector(appviewbtnClick:) forControlEvents:UIControlEventTouchUpInside];73        74     }75 }76 77 /**按鈕的點擊事件*/78 -(void)appviewbtnClick:(UIButton *)btn79 {80     YYapp *apps=self.app[btn.tag];81     UILabel *showlab=[[UILabel alloc]initWithFrame:CGRectMake(60, 450, 200, 20)];82     [showlab setText:[NSString stringWithFormat: @"%@下載成功",apps.name]];83     [showlab setBackgroundColor:[UIColor lightGrayColor]];84     [self.view addSubview:showlab];85     showlab.alpha=1.0;86     87     //簡單的動畫效果88     [UIView animateWithDuration:2.0 animations:^{89         showlab.alpha=0;90     } completion:^(BOOL finished) {91         [showlab removeFromSuperview];92     }];93 }94 95 @end

YYappview.h檔案代碼(已經連線)

#import <UIKit/UIKit.h>@interface YYappview : UIView@property (strong, nonatomic) IBOutlet UIImageView *appimg;@property (strong, nonatomic) IBOutlet UILabel *applab;@property (strong, nonatomic) IBOutlet UIButton *appbtn;@end

**IOS:xib檔案解析(xib和storyboard的比較,一個輕量級一個重量級)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.