xib的简单使用

来源:互联网 发布:心理变态 知乎 编辑:程序博客网 时间:2024/05/16 07:46

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 @end15 16 @implementation YYViewController17 18 //1.加载数据信息19 -(NSArray *)app20 {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)viewDidLoad37 {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 *)btn78 {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文件代码如下:

复制代码
 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 @interface YYViewController ()14 @property(nonatomic,strong)NSArray *app;15 @end16 17 @implementation YYViewController18 19 //1.加载数据信息20 -(NSArray *)app21 {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 in temparray) {29             [arrayM addObject:[YYapp appWithDict:dict]];30         }31         _app=arrayM;32     }33     return _app;34 }35 36 //创建界面原型37 - (void)viewDidLoad38 {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 *)btn79 {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>@interface YYappview : UIView@property (strong, nonatomic) IBOutlet UIImageView *appimg;@property (strong, nonatomic) IBOutlet UILabel *applab;@property (strong, nonatomic) IBOutlet UIButton *appbtn;@end

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 天涯明月刀刷本队友技能太花怎么办 手机收所有短信时显示被隐藏怎么办 div放图片多出的空白怎么办 我14岁射精让我记忆力差了怎么办 qq闪退聊天记录全没了怎么办 木瓜奇迹忘记升级过头转职了怎么办 孩子进入青春期什么坏事都干怎么办 母狗狗第一次来月经应该怎么办图 社保断了想补缴怎么办?这些要知道 换工作社保断了一个月怎么办 北京社保中间断了几个月怎么办 农保和社保都交了医保怎么办 如果交了社保结婚生孩子断了怎么办 南京社保中间断了几个月怎么办 mu大师等级技能点加点太慢怎么办 永恒纪元戒不是本职业套装石怎么办 全民奇迹忘记在哪个区了怎么办 全民奇迹安卓的忘记那个区了怎么办 大天使之剑h5所在服人少怎么办 买的裙子因为好看没有勇气穿怎么办 魅蓝s6锁屏密码忘了怎么办 u盘被占用不能安全弹出怎么办 洛克王国得到了魔攻巨蟹座怎么办 在育碧下载游戏下一半不下了怎么办 轩辕传奇单机版忘记哪个区了怎么办 登腾讯游戏动态密码啥意思怎么办 说了不该说的话别人不原谅怎么办 轩辕传奇手游金币用错了怎么办 神秘时代4法杖按键冲突怎么办 孕2个月发烧38度怎么办 不知道怀孕喝了止咳糖浆怎么办? 小孩刮头发的备皮刀割住手怎么办? 天梭手表里面的刻度掉了怎么办 国战天下手游帐号丢失怎么办 肺力咳合剂一次喝了50多了怎么办 头孢和藿香正气水一起吃了怎么办 小儿胃蛋自酶合剂吃多了怎么办 刚出生的婴儿很容易被惊醒怎么办 1个多月的宝宝小腿不直怎么办 20个月宝宝腿不直小腿外八怎么办 小孩手青枝骨骨折拆石膏还弯怎么办