当前位置:首页 > 操作系统 > Ios

iOS开发UI基础—xib的简单使用

iOS开发UI基础—xib的简单使用

一、简单介绍

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

二、xib的简单使用

1.建立xib文件

bubuko.com,布布扣

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

bubuko.com,布布扣

2.对xib进行设置

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

bubuko.com,布布扣

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

bubuko.com,布布扣

调整布局和内部的控件

bubuko.com,布布扣

 

完成后的单个view

bubuko.com,布布扣

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@end1516@implementation YYViewController
1718//1.加载数据信息19 -(NSArray *)app
20{
21if (!_app) {
22         NSString *path=[[NSBundle mainBundle]pathForResource:@"app.plist" ofType:nil];
23         NSArray *temparray=[NSArray arrayWithContentsOfFile:path];
2425//字典转模型26         NSMutableArray *arrayM=[NSMutableArray array ];
27for (NSDictionary *dict in temparray) {
28            [arrayM addObject:[YYapp appWithDict:dict]];
29        }
30         _app=arrayM;
31    }
32return _app;
33}
3435//创建界面原型36 - (void)viewDidLoad
37{
38    [super viewDidLoad];
39     NSLog(@"%d",self.app.count);
4041//九宫格布局42int totalloc=3;
43     CGFloat appviewW=80;
44     CGFloat appviewH=90;
45     CGFloat margin=(self.view.frame.size.width-totalloc*appviewW)/(totalloc+1);
4647int count=self.app.count;
48for (int i=0; i<count; i++) {
4950int row=i/totalloc;
51int loc=i%totalloc;
52         CGFloat appviewX=margin + (margin +appviewW)*loc;
53         CGFloat appviewY=margin + (margin +appviewH)*row;
54         YYapp *app=self.app[i];
5556//拿出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);
6162         UIImageView *appviewImg=(UIImageView *)[appview viewWithTag:1];
63         appviewImg.image=app.image;
6465         UILabel *appviewlab=(UILabel *)[appview viewWithTag:2];
66         appviewlab.text=app.name;
6768         UIButton *appviewbtn=(UIButton *)[appview viewWithTag:3];
69        [appviewbtn addTarget:self action:@selector(appviewbtnClick:) forControlEvents:UIControlEventTouchUpInside];
70         appviewbtn.tag=i;
7172        [self.view addSubview:appview];
73    }
74}
7576/**按钮的点击事件*/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;
8586//简单的动画效果87     [UIView animateWithDuration:2.0 animations:^{
88         showlab.alpha=0;
89     } completion:^(BOOL finished) {
90        [showlab removeFromSuperview];
91    }];
92}
9394@end

运行效果:

bubuko.com,布布扣

三、对xib进行连线示例

1.连线示例

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

bubuko.com,布布扣

 


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

bubuko.com,布布扣

把xib和视图类进行连线

bubuko.com,布布扣

注意:在使用中把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@end1617@implementation YYViewController
1819//1.加载数据信息20 -(NSArray *)app
21{
22if (!_app) {
23         NSString *path=[[NSBundle mainBundle]pathForResource:@"app.plist" ofType:nil];
24         NSArray *temparray=[NSArray arrayWithContentsOfFile:path];
2526//字典转模型27         NSMutableArray *arrayM=[NSMutableArray array ];
28for (NSDictionary *dict in temparray) {
29            [arrayM addObject:[YYapp appWithDict:dict]];
30        }
31         _app=arrayM;
32    }
33return _app;
34}
3536//创建界面原型37 - (void)viewDidLoad
38{
39    [super viewDidLoad];
40     NSLog(@"%d",self.app.count);
4142//九宫格布局43int totalloc=3;
44     CGFloat appviewW=80;
45     CGFloat appviewH=90;
46     CGFloat margin=(self.view.frame.size.width-totalloc*appviewW)/(totalloc+1);
4748int count=self.app.count;
49for (int i=0; i<count; i++) {
5051int row=i/totalloc;
52int loc=i%totalloc;
53         CGFloat appviewX=margin + (margin +appviewW)*loc;
54         CGFloat appviewY=margin + (margin +appviewH)*row;
55         YYapp *app=self.app[i];
5657//拿出xib视图58        NSArray  *apparray= [[NSBundle mainBundle]loadNibNamed:@"appxib" owner:nil options:nil];
5960//注意这里的类型名!
61//UIView *appview=[apparray firstObject];62         YYappview  *appview=[apparray firstObject];
6364//加载视图65         appview.frame=CGRectMake(appviewX, appviewY, appviewW, appviewH);
66          [self.view addSubview:appview];
6768         appview.appimg.image=app.image;
69         appview.applab.text=app.name;
70         appview.appbtn.tag=i;
7172        [ appview.appbtn addTarget:self action:@selector(appviewbtnClick:) forControlEvents:UIControlEventTouchUpInside];
7374    }
75}
7677/**按钮的点击事件*/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;
8687//简单的动画效果88     [UIView animateWithDuration:2.0 animations:^{
89         showlab.alpha=0;
90     } completion:^(BOOL finished) {
91        [showlab removeFromSuperview];
92    }];
93}
9495@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

 

 

 

原文:http://www.cnblogs.com/wendingding/p/3750378.html


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!