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文件代码如下:

 //
// 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=;
CGFloat appviewW=;
CGFloat appviewH=;
CGFloat margin=(self.view.frame.size.width-totalloc*appviewW)/(totalloc+); int count=self.app.count;
for (int i=; 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:];
appviewImg.image=app.image; UILabel *appviewlab=(UILabel *)[appview viewWithTag:];
appviewlab.text=app.name; UIButton *appviewbtn=(UIButton *)[appview viewWithTag:];
[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(, , , )];
[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=;
} 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=;
CGFloat appviewW=;
CGFloat appviewH=;
CGFloat margin=(self.view.frame.size.width-totalloc*appviewW)/(totalloc+); int count=self.app.count;
for (int i=; 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(, , , )];
[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=;
} 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的简单使用的更多相关文章

  1. iOS开发UI篇—使用xib自定义UItableviewcell实现一个简单的团购应用界面布局

    iOS开发UI篇—使用xib自定义UItableviewcell实现一个简单的团购应用界面布局 一.项目文件结构和plist文件 二.实现效果 三.代码示例 1.没有使用配套的类,而是直接使用xib文 ...

  2. ios开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布局

    本文转自 :http://www.cnblogs.com/wendingding/p/3761730.html ios开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布 ...

  3. iOS开发UI篇—多控制器和导航控制器简单介绍

    iOS开发UI篇—多控制器和导航控制器简单介绍 一.多控制器 一个iOS的app很少只由一个控制器组成,除非这个app极其简单.当app中有多个控制器的时候,我们就需要对这些控制器进行管理 有多个vi ...

  4. iOS开发UI篇—Quartz2D简单使用(三)

    iOS开发UI篇—Quartz2D简单使用(三) 一.通过slider控制圆的缩放 1.实现过程 新建一个项目,新建一个继承自UIview的类,并和storyboard中自定义的view进行关联. 界 ...

  5. iOS开发UI篇—UITabBarController简单介绍

    iOS开发UI篇—UITabBarController简单介绍 一.简单介绍 UITabBarController和UINavigationController类似,UITabBarControlle ...

  6. iOS开发UI篇—Modal简单介绍

    iOS开发UI篇—Modal简单介绍 一.简单介绍 除了push之外,还有另外一种控制器的切换方式,那就是Modal 任何控制器都能通过Modal的形式展⽰出来 Modal的默认效果:新控制器从屏幕的 ...

  7. iOS开发UI篇—简单的浏览器查看程序

    iOS开发UI篇—简单的浏览器查看程序 一.程序实现要求 1.要求 2. 界面分析 (1) 需要读取或修改属性的控件需要设置属性 序号标签 图片 图片描述 左边按钮 右边按钮 (2) 需要监听响应事件 ...

  8. iOS开发UI篇—Kvc简单介绍

    ios开发UI篇—Kvc简单介绍 一.KVC简单介绍 KVC key valued coding 键值编码 KVC通过键值间接编码 补充: 与KVC相对的时KVO,即key valued observ ...

  9. iOS开发UI篇—iOS开发中三种简单的动画设置

    iOS开发UI篇—iOS开发中三种简单的动画设置 [在ios开发中,动画是廉价的] 一.首尾式动画 代码示例: // beginAnimations表示此后的代码要“参与到”动画中 [UIView b ...

随机推荐

  1. Leetcode: 132 Pattern

    Given a sequence of n integers a1, a2, ..., an, a 132 pattern is a subsequence ai, aj, ak such that ...

  2. wampserver-----------如何设置wampserver在windows下开机自动启动。

    虽然很简单,但是还是做个记录.我的习惯,还是看图: 到你电脑的服务里面找到这两项然后点击右键属性,设置为自动.

  3. HttpClient_001_初步实现项目01的servlet,与项目02的servlet,之间数据访问

    HttpClient_001_初步实现项目01的servlet,与项目02的servlet,之间数据访问 代码下载地址: http://download.csdn.net/detail/poiuy19 ...

  4. 如何生成DLL文件

    1.打开项目工程,点击Rebuild 2.Rebuild成功后,打开该项目所在文件目录 3.在路径里,在bin->Debug文件下可以看到刚生成成功的dll文件.

  5. poj 1091 跳蚤

    跳蚤 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8482   Accepted: 2514 Description Z城 ...

  6. Linux -- 文件统计常用命令

    标签(空格分隔): Linux sort -- 文件内排序命令 sort将文件的每一行作为一个单位,相互比较,比较原则是从首字符向后,依次比较其ASCII码. 按每行升序排序: sort seq.tx ...

  7. Aggregation 聚集

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  8. INPUT只能输入数字

    input只能输入数字: (只能输入数字,并且输入的值不能大于99),但是这样有个问题,就是当输入非数字字符时,输入框中所有的字符都会被清除 <input type="text&quo ...

  9. react项目组件化思考

    三个原则 single store render from top immutable data single store,便于组件之间通信. render from top,因为store就一个,每 ...

  10. 关于autoptr

    参考自: http://www.cppblog.com/expter/archive/2009/03/29/78270.html auto_ptr是什么. 解释1.auto_ptr是一个管理指针的对象 ...