這個部落格申請了有一段時間了,覺得好像是該寫點什麼了。這篇文章主要是關於一些xib的簡單的用法,希望可以協助到剛剛使用xib的新手們。
什麼是xib? xib能做什麼?
用來描述軟體介面的檔案。
如果沒有xib,所有的介面都需要通過代碼來手動建立。
有了xib以後,可以在xib中進行可視化開發,然後載入xib檔案的時候,系統自動產生對應的代碼來建立介面。
與xib類似的還有storyboard檔案。xib和storyboard的比較,一個輕量級一個重量級。
共同點:
都用來描述軟體介面。都用Interface Builder工具來編輯
不同點:
Xib是輕量級的,用來描述局部的UI介面
Storyboard是重量級的,用來描述整個軟體的多個介面,並且能展示多個介面之間的跳轉關係
二、xib的簡單使用
1.建立xib檔案
建立的xib檔案命名為appxib.xib
2.對xib進行設定
根據程式的需要,這裡把view調整為自由布局
建立view模型(設定長寬等參數)
調整布局和內部的控制項
完成後的單個view
3.使用xib檔案的程式碼範例
YYViewController.m檔案代碼如下:
//// YYViewController.m// 10-xib檔案的使用//// Created by apple on 14-5-24.// Copyright (c) 2014年 itcase. All rights reserved.//#import "YYViewController.h"#import "YYapp.h"@interface YYViewController ()@property(nonatomic,strong)NSArray *app;@end@implementation YYViewController//1.載入資料資訊-(NSArray *)app{ if (!_app) { NSString *path=[[NSBundle mainBundle]pathForResource:@"app.plist" ofType:nil]; NSArray *temparray=[NSArray arrayWithContentsOfFile:path]; //字典轉模型 NSMutableArray *arrayM=[NSMutableArray array ]; for (NSDictionary *dict in temparray) { [arrayM addObject:[YYapp appWithDict:dict]]; } _app=arrayM; } return _app;}//建立介面原型- (void)viewDidLoad{ [super viewDidLoad]; NSLog(@"%d",self.app.count); //九宮格布局 int totalloc=3; CGFloat appviewW=80; CGFloat appviewH=90; CGFloat margin=(self.view.frame.size.width-totalloc*appviewW)/(totalloc+1); int count=self.app.count; for (int i=0; i<count; i++) { int row=i/totalloc; int loc=i%totalloc; CGFloat appviewX=margin + (margin +appviewW)*loc; CGFloat appviewY=margin + (margin +appviewH)*row; YYapp *app=self.app[i]; //拿出xib視圖 NSArray *apparray= [[NSBundle mainBundle]loadNibNamed:@"appxib" owner:nil options:nil]; UIView *appview=[apparray firstObject]; //載入視圖 appview.frame=CGRectMake(appviewX, appviewY, appviewW, appviewH); UIImageView *appviewImg=(UIImageView *)[appview viewWithTag:1]; appviewImg.image=app.image; UILabel *appviewlab=(UILabel *)[appview viewWithTag:2]; appviewlab.text=app.name; UIButton *appviewbtn=(UIButton *)[appview viewWithTag:3]; [appviewbtn addTarget:self action:@selector(appviewbtnClick:) forControlEvents:UIControlEventTouchUpInside]; appviewbtn.tag=i; [self.view addSubview:appview]; }}/**按鈕的點擊事件*/-(void)appviewbtnClick:(UIButton *)btn{ YYapp *apps=self.app[btn.tag]; UILabel *showlab=[[UILabel alloc]initWithFrame:CGRectMake(60, 450, 200, 20)]; [showlab setText:[NSString stringWithFormat: @"%@下載成功",apps.name]]; [showlab setBackgroundColor:[UIColor lightGrayColor]]; [self.view addSubview:showlab]; showlab.alpha=1.0; //簡單的動畫效果 [UIView animateWithDuration:2.0 animations:^{ showlab.alpha=0; } completion:^(BOOL finished) { [showlab removeFromSuperview]; }];}@end
運行效果:
三、對xib進行連線樣本
1.連線樣本
建立一個xib對應的視圖類,繼承自Uiview
在xib介面右上方與建立的視圖類進行關聯
把xib和視圖類進行連線
注意:在使用中把weak改成為強引用。否則...
2.連線後的程式碼範例
YYViewController.m檔案代碼如下:
//// YYViewController.m// 10-xib檔案的使用//// Created by apple on 14-5-24.// Copyright (c) 2014年 itcase. All rights reserved.//#import "YYViewController.h"#import "YYapp.h"#import "YYappview.h"@interface YYViewController ()@property(nonatomic,strong)NSArray *app;@end@implementation YYViewController//1.載入資料資訊-(NSArray *)app{ if (!_app) { NSString *path=[[NSBundle mainBundle]pathForResource:@"app.plist" ofType:nil]; NSArray *temparray=[NSArray arrayWithContentsOfFile:path]; //字典轉模型 NSMutableArray *arrayM=[NSMutableArray array ]; for (NSDictionary *dict in temparray) { [arrayM addObject:[YYapp appWithDict:dict]]; } _app=arrayM; } return _app;}//建立介面原型- (void)viewDidLoad{ [super viewDidLoad]; NSLog(@"%d",self.app.count); //九宮格布局 int totalloc=3; CGFloat appviewW=80; CGFloat appviewH=90; CGFloat margin=(self.view.frame.size.width-totalloc*appviewW)/(totalloc+1); int count=self.app.count; for (int i=0; i<count; i++) { int row=i/totalloc; int loc=i%totalloc; CGFloat appviewX=margin + (margin +appviewW)*loc; CGFloat appviewY=margin + (margin +appviewH)*row; YYapp *app=self.app[i]; //拿出xib視圖 NSArray *apparray= [[NSBundle mainBundle]loadNibNamed:@"appxib" owner:nil options:nil]; //注意這裡的類型名! //UIView *appview=[apparray firstObject]; YYappview *appview=[apparray firstObject]; //載入視圖 appview.frame=CGRectMake(appviewX, appviewY, appviewW, appviewH); [self.view addSubview:appview]; appview.appimg.image=app.image; appview.applab.text=app.name; appview.appbtn.tag=i; [ appview.appbtn addTarget:self action:@selector(appviewbtnClick:) forControlEvents:UIControlEventTouchUpInside]; }}/**按鈕的點擊事件*/-(void)appviewbtnClick:(UIButton *)btn{ YYapp *apps=self.app[btn.tag]; UILabel *showlab=[[UILabel alloc]initWithFrame:CGRectMake(60, 450, 200, 20)]; [showlab setText:[NSString stringWithFormat: @"%@下載成功",apps.name]]; [showlab setBackgroundColor:[UIColor lightGrayColor]]; [self.view addSubview:showlab]; showlab.alpha=1.0; //簡單的動畫效果 [UIView animateWithDuration:2.0 animations:^{ showlab.alpha=0; } completion:^(BOOL finished) { [showlab removeFromSuperview]; }];}@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
以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援雲棲社區。