//Xibs就是佈局界面的一種方式。ios
//建立xib的方法:選擇New->File->UserInterface->Empty 設置將要建立Xib的文件名字,框架
//============ 第一種: 首先,關於Xib加載 這裏以「XibLoadView」爲例==========//ide
//--------------進入XibLoadView.xib文件-----------佈局
//建立好Xib文件後,咱們看到Xib中沒有任何頁面,拖進一個View控件,此時View控件不能調節大小,設置View的size是Freeform,拖入應有的控件。atom
//--------------進入Viewcontroller.m文件------------spa
//1.加載Xib文件。.net
/*3d
bundle是一個目錄,其中包含了程序會使用到的資源. 這些資源包含了如圖像,聲音,編譯好的代碼,nib文件(用戶也會把bundle稱爲plug-in). 對應bundle,cocoa提供了類NSBundle.orm
咱們的程序是一個bundle. 在Finder中,一個應用程序看上去和其餘文件沒有什麼區別. 可是實際上它是一個包含了nib文件,編譯代碼,以及其餘資源的目錄. 咱們把這個目錄叫作程序的main bundle對象
經過使用下面的方法獲得程序的main bundle
NSBundle *myBundle = [NSBundle mainBundle];//通常咱們經過這種方法來獲得bundle.
一旦咱們有了NSBundle 對象,那麼就能夠訪問其中的資源了
*/
//加載方法:[ [ NSBundle mainBundle ] loadNibNamed:@"Xib文件名" owner:nil options:nil ] ;
//============第二種 其次,關於Xib控件屬性 這裏以「XibViewOne」爲例==========//
//與第一種方法的不一樣,這樣的Xib能夠得到控件的對象,設置每個控件的屬性
//--------------進入XibLoadView.xib文件-----------
//建立好Xib文件後,咱們看到Xib中沒有任何頁面,拖進一個View控件,此時View控件不能調節大小,設置View1的size是Freeform後在看,這是View就能夠改變大小了。調整好View的大小。設置backgroundColor的顏色以便確認是否加載成功。拖Label控件標示說明當前View
//--------------進入Viewcontroller.h文件------------
//拖線,將控件拖進viewController的接口文件裏,注意要將file's owner的class變爲ViewController.
//--------------進入Viewcontroller.m文件------------
//加載Xib。注意owner變量爲self
//============第三種 關於Xib控件 這裏以「XibViewTwo」爲例,建立Class名爲ViewTwo==========//
//這種是將Xib文件和Class分開建立,建好後在進行變量關聯的
//--------------進入XibLoadView.xib文件-----------
//建立好Xib文件後,拖入須要控件,將控件大小設置好,設置文件的File's Owner 對應的Class。設置欄填寫ViewTwo
//--------------進入ViewTwo.h文件------------
//拖線。注意:若是建立的爲NSObject類須要引入UIKit框架
//--------------進入Viewcontroller.m文件------------
//1.引入頭文件,ViewTwo.h 2.實例化ViewTwo的對象
//2.加載Xib。注意owner變量爲ViewTwo的對象
//3.獲取View
#import "ViewController.h"
#import "ViewTwo.h"
@interface ViewController ()
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//============ 關於Xib加載 ==========//
//加載Xib。當Xib在通過編譯連接以後就變爲nib文件。nib在bundle目錄下,先獲取程序的mainbundle,在其中加載指定的nib文件,這裏owner爲nil是因爲,沒有owner變量
NSArray * array = [ [ NSBundle mainBundle ] loadNibNamed:@"XibLoadView" owner:nil options:nil ] ;
UIView * ViewLoadFromXib = [ array firstObject ] ;//在取出View時注意,這裏咱們能夠用[ array lastObject ],因爲這個Xib只有一個View
[ self.view addSubview : ViewLoadFromXib ] ;//添加到根視圖
//============ 其次,關於Xib控件屬性 這裏以「XibViewOne」爲例==========//
NSArray * arrayOne = [ [ NSBundle mainBundle ] loadNibNamed:@"XibViewOne" owner:self options:nil ] ;//這裏的owner是
_viewOne = [ arrayOne firstObject ] ;//在取出View時注意,這裏咱們能夠用[ array lastObject ],因爲這個Xib只有一個View
_viewOne.frame=CGRectMake(0, 100, 375, 100);
[ self.view addSubview : _viewOne ] ;//添加到根視圖
//============ 關於Xib控件 這裏以「XibViewTwo」爲例==========//
//實例化ViewTwo對象
ViewTwo * viewOnwer = [ [ ViewTwo alloc ] init ] ;
NSArray * arrayViewTwo = [[ NSBundle mainBundle ] loadNibNamed:@"XibViewTwo" owner:viewOnwer options:nil];
UIView * view = [ arrayViewTwo firstObject ] ;
view.frame=CGRectMake(0, 210, 375, 100);
viewOnwer.label.text=@"XibViewTwo-___-";
[ self.view addSubview:view ] ;
}
@property (weak, nonatomic) IBOutlet UIView *viewOne;
@property (weak, nonatomic) IBOutlet UILabel *lable;
//ViewTwo.h
@property (weak, nonatomic) IBOutlet UILabel *label;
@property (strong, nonatomic) IBOutlet UIView *View;
//XIB
- (void)viewDidLoad {
[super viewDidLoad];
ViewOnwer *onwer=[[ViewOnwer alloc]init];
UIScrollView * scrollView=[[UIScrollView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
for (int i=0; i<6; i++) {
NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"View" owner:onwer options:nil];
UIView * view =[array firstObject];
onwer.label.text=@"移動迷宮2";
onwer.image.image=[UIImage imageNamed:@"2"];
[onwer.button setTitle:@"下載" forState:UIControlStateNormal];
[onwer.button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[onwer.button addTarget:self action:@selector(Down:) forControlEvents:UIControlEventTouchUpInside];
view.frame = CGRectMake( 0, i * view.frame.size.height, view.frame.size.width, view.frame.size.height);
[scrollView addSubview:view];
}
scrollView.contentSize=CGSizeMake(0,onwer.viewCell.frame.size.height * 6 );
[self.view addSubview:scrollView];
}
-(void)Down:(UIButton *)sender{
[sender setTitle:@"正在下載" forState:UIControlStateNormal];
}
@interface ViewOnwer : NSObject
@property (weak, nonatomic) IBOutlet UIImageView *image;
@property (weak, nonatomic) IBOutlet UILabel *label;
@property (weak, nonatomic) IBOutlet UIButton *button;
@property (strong, nonatomic) IBOutlet UIView *viewCell;
//在列表裏樣式有不少種,咱們要注意通常的TableView是沒有圖片的,當咱們須要某種樣式的TableViewCell。咱們能夠藉助繼承UITableViewCell類的來建立本身的tableView
//1.New File -> ios Souce -> Cocoa Touch Class (UITableViewCell爲父類)注意勾選同時建立Xib選項
//2.在建立的類裏重寫實例化方法(-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reusename)-----去TableViewCell.m裏看~
//3.有cell就要相應的容器裝,建立UITableView。回到ViewCintroller的ViewDidLoad方法。
//4.遵照協議(兩個UITableViewDelegate和UITableViewDataSource),設置實現者
//5.實現必要方法
//-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
//-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
//6.繪製cell的時候注意這是的cell的樣式是咱們設置好的,將除去本來方法返回外,全部UITableViewCell的類轉化爲自定義類
//7.調整行距。-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
#import "ViewController.h"
#import "TableViewCell.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UITableView *tableView=[[UITableView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
tableView.delegate=self;
tableView.dataSource=self;
[self.view addSubview:tableView];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 4;
}
//繪製cell的方法
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
TableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell"];
if (cell==nil) {
cell=[[TableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}
cell.cellLabel.text=@"hello";
cell.cellImage.image=[UIImage imageNamed:@"0"];
return cell;
}
//7.調整行距。這是設置行高的方法
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 50;
}
@implementation TableViewCell
//這是同時建立的,不用修改Class!!!
//拖拽注意控件的Subject爲TableViewCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reusename{
self=[super initWithStyle:style reuseIdentifier:reusename];//建立的子類的對象要實現父類的方法
NSArray * array = [[NSBundle mainBundle]loadNibNamed:@"TableViewCell" owner:self options:nil];//加載Xib
self=[array firstObject];//返回的View爲Xib設置好的
return self;//返回本身設置好的行
}