xcode xib的代码创建小事例

创建一个 xib  我的命名wlisEmpty.xib

并且我设置了wlisEmpty.xib 用到的类

同时创建一个类wlisEmpty.h   我在.h文件中只应用了模型数据    

 

#import <UIKit/UIKit.h>
@class  wlisZuoye;
@interface wlisEmpty : UIView


@property(nonatomic,strong)wlisZuoye * zuoye;
@end
View Code
#import "wlisEmpty.h"
#import "wlisZuoye.h"
@interface wlisEmpty ()
@property (weak, nonatomic) IBOutlet UIImageView *imgView;
@property (weak, nonatomic) IBOutlet UILabel *labBanner;

@property (weak, nonatomic) IBOutlet UILabel *labContent;
@property (weak, nonatomic) IBOutlet UIButton *btnFollowUp;

@end
@implementation wlisEmpty

-(void)layoutSubviews{
    [super layoutSubviews];
    CGFloat cgW=self.frame.size.width;
    CGFloat cgH=self.frame.size.height;
    //设置图片的位置尺寸
    self.imgView.frame=CGRectMake(0, 0, 96, cgH);
    //设置标题的位置尺寸
    self.labBanner.frame=CGRectMake(self.imgView.frame.size.width, 0, cgW-self.imgView.frame.size.width, 30);
    //设置内容的位置尺寸
    self.labContent.frame=CGRectMake(self.imgView.frame.size.width, self.labBanner.frame.size.height, cgW-self.imgView.frame.size.width, cgH-self.labBanner.frame.size.height);
    //设置按钮的位置尺寸
    self.btnFollowUp.frame=CGRectMake(cgW-55, cgH-20, 55, 20);
    self.btnFollowUp.layer.borderColor=[[UIColor blackColor]CGColor];
    self.btnFollowUp.layer.borderWidth=0.5;
    //设置边框宽度
    self.layer.borderWidth=1;
    //设置边框颜色
    self.layer.borderColor=[[UIColor lightGrayColor]CGColor];
    self.labContent.numberOfLines=2;
    self.btnFollowUp.titleLabel.font =[UIFont systemFontOfSize:9.0];
    self.imgView.layer.borderWidth=2;
    self.imgView.layer.borderColor=[[UIColor blackColor]CGColor];
}

- (void)setZuoye:(wlisZuoye *)zuoye{
    self->_zuoye=zuoye;
    //设置图片
    [self->_imgView setImage:[UIImage imageNamed:zuoye.image]];
    //设置标题
    [self->_labBanner setText:zuoye.banner];
    //设置内容
    [self->_labContent setText:zuoye.content];
    //设置按钮文字
    [self->_btnFollowUp setTitle:zuoye.followUp forState:UIControlStateNormal];
}
@end
wlisEmpty.m

弄完以上的直接上控制器的代码

#import "ViewController.h"
#import "wlisZuoye.h"
#import "wlisEmpty.h"
@interface ViewController ()
/**
 图片
 标题
 内容
 跟帖
 一个容器
 */

@property(nonatomic,strong) UIImageView * img_image;
@property(nonatomic,strong) UILabel * labbanner;
@property(nonatomic,strong)UILabel * labcontent;
@property(nonatomic,strong)UIButton * btn;
@property(nonatomic,strong)UIView * view_1;
@property(nonatomic,strong)NSArray * arrRun;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
 
 /*
  创建run.plist文件
    NSArray * arry=@[
                     @{@"banner":@"随意1",@"content":@"收到货了就和我二姐呢桑德环境禄口机场是两地分居玩儿就能看到发送到;开发商的黑龙江省的客户机",@"image":@"run_1"},
                     @{@"banner":@"等风2",@"content":@"等风到的日子里,先从不等待开始,所以春风来不来真的没有什么关系",@"image":@"run_2"}
                     ];
    
    BOOL isbool=[arry writeToFile:@"/Users/xmg/Desktop/run.plist" atomically:YES];
    if (isbool) {
        NSLog(@"成功创见");
    }
  
   */
    
    //获取屏幕宽度
    CGFloat cgW=self.view.frame.size.width;
 
    //从模型中获取数据
    wlisZuoye * wl1=self.arrRun[0];
    wlisZuoye * wl2=self.arrRun[1];
    
    /**
     第一行数据
     */
    NSArray * arr=[[NSBundle mainBundle]loadNibNamed:@"wlisEmpty" owner:nil options:nil];
    wlisEmpty * showView=[arr lastObject];
    showView.frame=CGRectMake(0, 20, cgW, 100);
    [self.view addSubview:showView];
    showView.zuoye=wl1;
    
    /**
     第二行数据
     */
    wlisEmpty * showView1=[[[NSBundle mainBundle]loadNibNamed:@"wlisEmpty" owner:nil options:nil]lastObject];
    showView1.frame=CGRectMake(0, 20+100+2, cgW, 100);
    [self.view addSubview:showView1];
    showView1.zuoye=wl2;

    
   }

/**
 重写arrRun get方法
 */
- (NSArray *)arrRun{
    //判断数据是否为空
    if (self->_arrRun==nil) {
        //按名称获取数据的路径
        NSString * path=[[NSBundle mainBundle]pathForResource:@"run.plist" ofType:nil];
        //数据加载到数组中
        NSArray * arr=[NSArray arrayWithContentsOfFile:path];
        //创建一个可变的数组
        NSMutableArray *arrM=[NSMutableArray array];
        //遍历数组  转成模型对象加载到数组中
        for (NSDictionary * dic in arr) {
            [arrM addObject:[wlisZuoye wlisZuoyeWithDic:dic]];
        }
        //给数组赋值
        self->_arrRun=arrM;
        
    }
    //返回数组
    return self->_arrRun;
}

@end
ViewController

 

posted @ 2016-06-04 21:42  与格律上  阅读(114)  评论(0)    收藏  举报