iOS開(kāi)發(fā)UI篇—xib的簡(jiǎn)單使用實(shí)例
這個(gè)博客申請(qǐng)了有一段時(shí)間了,覺(jué)得好像是該寫(xiě)點(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)行可視化開(kāi)發(fā),然后加載xib文件的時(shí)候,系統(tǒng)自動(dòng)生成對(duì)應(yīng)的代碼來(lái)創(chuàng)建界面。
與xib類(lèi)似的還有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)畫(huà)效果
[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)的視圖類(lèi),繼承自Uiview
在xib界面右上角與新建的視圖類(lèi)進(jìn)行關(guān)聯(lián)
把xib和視圖類(lèi)進(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];
//注意這里的類(lèi)型名!
//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)畫(huà)效果
[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í)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
iOS UICollectionView實(shí)現(xiàn)橫向滑動(dòng)
這篇文章主要為大家詳細(xì)介紹了iOS UICollectionView實(shí)現(xiàn)橫向滑動(dòng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-03-03
iPhoneX無(wú)導(dǎo)航欄頁(yè)面適配問(wèn)題解決方案
這篇文章主要介紹了iPhoneX無(wú)導(dǎo)航欄頁(yè)面適配問(wèn)題解決方案,原全屏適配在iPhoneX會(huì)由于安全區(qū)域的變化導(dǎo)致顯示不全,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2017-11-11
IOS 自定義UICollectionView的頭視圖或者尾視圖UICollectionReusableView
這篇文章主要介紹了IOS 自定義UICollectionView的頭視圖或者尾視圖UICollectionReusableView的相關(guān)資料,需要的朋友可以參考下2017-01-01
swift3.0實(shí)現(xiàn)圖片放大縮小動(dòng)畫(huà)效果
這篇文章主要為大家詳細(xì)介紹了swift3.0實(shí)現(xiàn)圖片放大縮小動(dòng)畫(huà)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09
淺談iphone X的簡(jiǎn)單適配問(wèn)題(推薦)
這篇文章主要介紹了淺談iphone X的簡(jiǎn)單適配(推薦),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11
searchDisplayController 引起的數(shù)組越界處理辦法
這篇文章主要介紹了searchDisplayController 引起的數(shù)組越界處理辦法,需要的朋友可以參考下2015-07-07

