iOS开发UI篇—xib的简单使用

摘要:
一、简单介绍xib和storyboard的比较,一个轻量级一个重量级。

一、简单介绍

xib和storyboard的比较,一个轻量级一个重量级。

共同点:

都用来描述软件界面

都用Interface Builder工具来编辑

不同点:

Xib是轻量级的,用来描述局部的UI界面

Storyboard是重量级的,用来描述整个软件的多个界面,并且能展示多个界面之间的跳转关系

二、xib的简单使用

1.建立xib文件

iOS开发UI篇—xib的简单使用第1张

建立的xib文件命名为appxib.xib

iOS开发UI篇—xib的简单使用第2张

2.对xib进行设置

根据程序的需要,这里把view调整为自由布局

iOS开发UI篇—xib的简单使用第3张

建立view模型(设置长宽等参数)

iOS开发UI篇—xib的简单使用第4张

调整布局和内部的控件

iOS开发UI篇—xib的简单使用第5张

完成后的单个view

iOS开发UI篇—xib的简单使用第6张

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 @interfaceYYViewController ()
13 @property(nonatomic,strong)NSArray *app;
14 @end
15 
16 @implementationYYViewController
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 intemparray) {
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

运行效果:

iOS开发UI篇—xib的简单使用第7张

三、对xib进行连线示例

1.连线示例

新建一个xib对应的视图类,继承自Uiview

iOS开发UI篇—xib的简单使用第8张


在xib界面右上角与新建的视图类进行关联

iOS开发UI篇—xib的简单使用第9张

把xib和视图类进行连线

iOS开发UI篇—xib的简单使用第10张

2.连线后的代码示例

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 #import "YYappview.h"
12 
13 @interfaceYYViewController ()
14 @property(nonatomic,strong)NSArray *app;
15 @end
16 
17 @implementationYYViewController
18 
19 //1.加载数据信息
20 -(NSArray *)app
21 {
22     if (!_app) {
23         NSString *path=[[NSBundle mainBundle]pathForResource:@"app.plist"ofType:nil];
24         NSArray *temparray=[NSArray arrayWithContentsOfFile:path];
25         
26         //字典转模型
27         NSMutableArray *arrayM=[NSMutableArray array ];
28         for (NSDictionary *dict intemparray) {
29 [arrayM addObject:[YYapp appWithDict:dict]];
30 }
31         _app=arrayM;
32 }
33     return_app;
34 }
35 
36 //创建界面原型
37 - (void)viewDidLoad
38 {
39 [super viewDidLoad];
40     NSLog(@"%d",self.app.count);
41     
42     //九宫格布局
43     int totalloc=3;
44     CGFloat appviewW=80;
45     CGFloat appviewH=90;
46     CGFloat margin=(self.view.frame.size.width-totalloc*appviewW)/(totalloc+1);
47     
48     int count=self.app.count;
49     for (int i=0; i<count; i++) {
50         
51         int row=i/totalloc;
52         int loc=i%totalloc;
53         CGFloat appviewX=margin + (margin +appviewW)*loc;
54         CGFloat appviewY=margin + (margin +appviewH)*row;
55         YYapp *app=self.app[i];
56         
57         //拿出xib视图
58        NSArray  *apparray= [[NSBundle mainBundle]loadNibNamed:@"appxib"owner:nil options:nil];
59         
60         //注意这里的类型名!
61         //UIView *appview=[apparray firstObject];
62         YYappview  *appview=[apparray firstObject];
63        
64         //加载视图
65         appview.frame=CGRectMake(appviewX, appviewY, appviewW, appviewH);
66 [self.view addSubview:appview];
67         
68         appview.appimg.image=app.image;
69         appview.applab.text=app.name;
70         appview.appbtn.tag=i;
71         
72 [ appview.appbtn addTarget:self action:@selector(appviewbtnClick:) forControlEvents:UIControlEventTouchUpInside];
73        
74 }
75 }
76 
77 /**按钮的点击事件*/
78 -(void)appviewbtnClick:(UIButton *)btn
79 {
80     YYapp *apps=self.app[btn.tag];
81     UILabel *showlab=[[UILabel alloc]initWithFrame:CGRectMake(60, 450, 200, 20)];
82     [showlab setText:[NSString stringWithFormat: @"%@下载成功",apps.name]];
83 [showlab setBackgroundColor:[UIColor lightGrayColor]];
84 [self.view addSubview:showlab];
85     showlab.alpha=1.0;
86     
87     //简单的动画效果
88     [UIView animateWithDuration:2.0 animations:^{
89         showlab.alpha=0;
90     } completion:^(BOOL finished) {
91 [showlab removeFromSuperview];
92 }];
93 }
94 
95 @end

YYappview.h文件代码(已经连线)

#import <UIKit/UIKit.h>

@interfaceYYappview : UIView
@property (weak, nonatomic) IBOutlet UIImageView *appimg;
@property (weak, nonatomic) IBOutlet UILabel *applab;
@property (weak, nonatomic) IBOutlet UIButton *appbtn;
@end

免责声明:文章转载自《iOS开发UI篇—xib的简单使用》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇BitConverter.GetBytes(int)和BitConverter.ToString 方法 (Byte[])linux 文件系统之superblock下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

uniapp开发支付宝小程序,ios软键盘会把底部fixed定位的输入框覆盖掉有效解决方法

这个问题应该可以定义为ios的兼容性、目前测试的只有在ios上才会有这种情况 之前百度找的用uniapp里面的参数输入框设置cursor-spacing="0" 或者等于其他数值、然后基本无效 解决方法就一行css代码就可以了、把外层高度设置为100vh,就不会出现刚刚的兼容性,我的是tbody设置的h:100vh,cont靠内容自己撑起来 然后我html...

iOS开发基础知识--碎片26

  iOS开发基础知识--碎片26  1:UICollectionView如果在数据不够一屏时上下滚动 当数据不多,collectionView.contentSize小于collectionView.frame.size的时候,UICollectionView是不会滚动的,可以增加下面代码就可以: self.myCollectionView.alwa...

Qt qss一些伪装态,以及margin与padding区别

伪状态 描述 :checked button部件被选中:disabled 部件被禁用:enabled 部件被启用:focus 部件获得焦点:hover 鼠标位于部件上:indeterminate checkbox或radiobutton被部分选中:off 部件可以切换,且处于off状态:on 部件可以切换,且处于on状态:pressed 部件被鼠标按下:u...

李洪强iOS开发之基于彻底解耦合的实验性iOS架构

基于彻底解耦合的实验性iOS架构 这周我决定做一个关于彻底解耦合的应用架构的实验。我想探究的主题是: “如果所有的应用内通讯都通过一个事件流来完成会怎么样?” 我构造了一个待办事项应用,因为这是我一时激动下所能想到的最原始微型的项目。我会大概地说一下应用结构背后的想法,展示具体实现中的一些代码片段,然后给出几个有关利弊的结论。 整个项目在Gi...

Windows App开发之集成设置、帮助、搜索和共享

应用设置和应用帮助 ”设置“合约 上一节中我们学习了如何将应用设置保存到本地,这种方式是通过在App内添加设置选项,这里还有一种方式。微软将其称为“设置”合约,并且所有的Windows应用商店应用都将自动配合这种合约。但是应用自带的这种设置如果不做任何修改可谓毫无作用。而我们添加这些设置则可以让应用更加个性化哦。 SettingsFlyout...

在Visual Studio中使用MonoTouch开发iOS应用程序(下):开发体验 狼人:

对于熟悉.NET程序员来说,编写iOS应用程序的最佳选择自然是MonoTouch。在上一篇文章里,我们已经在Mac OS X上安装了MonoTouch开发环境,并已经能够在Mac OS X和Windows之间共享文件。现在我们就可以来简单体验一下,如何使用Visual Studio,Interface Builder以及少量的MonoDevelop来开发一...