1. 程式人生 > >iOS開發UI篇—xib的簡單使用

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

board ani uila tro 字典 tex mat inter .com

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 @end
15 
16 @implementation YYViewController
17 
18 //1.加載數據信息
19 -(NSArray *)app
20 {
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)viewDidLoad
37 {
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 *)btn
78 {
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文件代碼如下:

技術分享圖片

//
// 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

技術分享圖片

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