這個(gè)博客申請(qǐng)了有一段時(shí)間了,覺(jué)得好像是該寫點(diǎn)什么了。這篇文章主要是關(guān)于一些xib的簡(jiǎn)單的用法,希望可以幫助到剛剛使用xib的新手們。
什么是xib? xib能做什么?
用來(lái)描述軟件界面的文件。
如果沒(méi)有xib,所有的界面都需要通過(guò)代碼來(lái)手動(dòng)創(chuàng)建。
有了xib以后,可以在xib中進(jìn)行可視化開發(fā),然后加載xib文件的時(shí)候,系統(tǒng)自動(dòng)生成對(duì)應(yīng)的代碼來(lái)創(chuàng)建界面。
與xib類似的還有storyboard文件。xib和storyboard的比較,一個(gè)輕量級(jí)一個(gè)重量級(jí)。
共同點(diǎn):
都用來(lái)描述軟件界面。都用Interface Builder工具來(lái)編輯
不同點(diǎn):
Xib是輕量級(jí)的,用來(lái)描述局部的UI界面
Storyboard是重量級(jí)的,用來(lái)描述整個(gè)軟件的多個(gè)界面,并且能展示多個(gè)界面之間的跳轉(zhuǎn)關(guān)系
二、xib的簡(jiǎn)單使用
1.建立xib文件
建立的xib文件命名為appxib.xib
2.對(duì)xib進(jìn)行設(shè)置
根據(jù)程序的需要,這里把view調(diào)整為自由布局
建立view模型(設(shè)置長(zhǎng)寬等參數(shù))
調(diào)整布局和內(nèi)部的控件
完成后的單個(gè)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.加載數(shù)據(jù)信息-(NSArray *)app{ if (!_app) { NSString *path=[[NSBundle mainBundle]pathForResource:@"app.plist" ofType:nil]; NSArray *temparray=[NSArray arrayWithContentsOfFile:path]; //字典轉(zhuǎn)模型 NSMutableArray *arrayM=[NSMutableArray array ]; for (NSDictionary *dict in temparray) { [arrayM addObject:[YYapp appWithDict:dict]]; } _app=arrayM; } return _app;}//創(chuàng)建界面原型- (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]; }}/**按鈕的點(diǎn)擊事件*/-(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; //簡(jiǎn)單的動(dòng)畫效果 [UIView animateWithDuration:2.0 animations:^{ showlab.alpha=0; } completion:^(BOOL finished) { [showlab removeFromSuperview]; }];}@end
運(yùn)行效果:
三、對(duì)xib進(jìn)行連線示例
1.連線示例
新建一個(gè)xib對(duì)應(yīng)的視圖類,繼承自Uiview
在xib界面右上角與新建的視圖類進(jìn)行關(guān)聯(lián)
把xib和視圖類進(jìn)行連線
注意:在使用中把weak改成為強(qiáng)引用。否則...
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.加載數(shù)據(jù)信息-(NSArray *)app{ if (!_app) { NSString *path=[[NSBundle mainBundle]pathForResource:@"app.plist" ofType:nil]; NSArray *temparray=[NSArray arrayWithContentsOfFile:path]; //字典轉(zhuǎn)模型 NSMutableArray *arrayM=[NSMutableArray array ]; for (NSDictionary *dict in temparray) { [arrayM addObject:[YYapp appWithDict:dict]]; } _app=arrayM; } return _app;}//創(chuàng)建界面原型- (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]; }}/**按鈕的點(diǎn)擊事件*/-(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; //簡(jiǎn)單的動(dòng)畫效果 [UIView animateWithDuration:2.0 animations:^{ showlab.alpha=0; } completion:^(BOOL finished) { [showlab removeFromSuperview]; }];}@end
YYappview.h文件代碼(已經(jīng)連線)
#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
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。
新聞熱點(diǎn)
疑難解答
圖片精選