1,自定义view继承uiview。创建xib文件绑定该view以及元素2,在自定义view内部创建一个初始化方法来获取xib布局主要方法: NSArray* nibView = [[NSBundle mainBundle] loadNibNamed:@"CenterView" owner:nil options:nil];CenterView *cv =[nibView objectAtIndex:0];下面附上创建的代码CenterView.h#import@interface CenterView : UIView
+(CenterView *)instanceCenterViewWithType:(NSString*)type;
@property (retain, nonatomic) IBOutlet UIImageView *img1;
@property (retain, nonatomic) IBOutlet UIImageView *img2;
@property (retain, nonatomic) IBOutlet UIImageView *img3;
@property (retain, nonatomic) IBOutlet UIImageView *img4;
@property (retain, nonatomic) IBOutlet UIImageView *img5;
@property (retain, nonatomic) IBOutlet UIImageView *img6;
@property (retain, nonatomic) IBOutlet UILabel *lb1;
@property (retain, nonatomic) IBOutlet UILabel *lb2;
@property (retain, nonatomic) IBOutlet UILabel *lb3;
@property (retain, nonatomic) IBOutlet UILabel *lb4;
@property (retain, nonatomic) IBOutlet UILabel *lb5;
@property (retain, nonatomic) IBOutlet UILabel *lb6;
@property (retain, nonatomic) IBOutlet UIView *contentview;
@property (retain, nonatomic) IBOutlet UIView *tv1;
@property (retain, nonatomic) IBOutlet UIView *tv2;
@property (retain, nonatomic) IBOutlet UIView *tv3;
@property (retain, nonatomic) IBOutlet UIView *tv5;
@property (retain, nonatomic) IBOutlet UIView *tv6;
@property (retain, nonatomic) IBOutlet UIView *tv4;
@end
CenterView.m
#import "CenterView.h"
@implementation CenterView
+(CenterView *)instanceCenterViewWithType:(NSString*)type
{
NSArray* nibView = [[NSBundle mainBundle] loadNibNamed:@"CenterView" owner:nil options:nil];
CenterView *cv =[nibView objectAtIndex:0];
if ([type isEqualToString:@"zx"]) {
cv.img1.image = [UIImage imageNamed:@"zx1.png"];
cv.img2.image = [UIImage imageNamed:@"zx2.png"];
cv.img3.image = [UIImage imageNamed:@"zx3.png"];
cv.img4.image = [UIImage imageNamed:@"zx4.png"];
cv.img5.image = [UIImage imageNamed:@"zx5.png"];
cv.img6.image = [UIImage imageNamed:@"zx6.png"];
cv.lb1.text = @"统计中心";
cv.lb2.text = @"通知公告";
cv.lb3.text = @"政策指南";
cv.lb4.text = @"企业搜索";
cv.lb5.text = @"机构展示";
cv.lb6.text = @"企业展示";
}else if([type isEqualToString:@"jg"]){
cv.img1.image = [UIImage imageNamed:@"jg1.png"];
cv.img2.image = [UIImage imageNamed:@"jg2.png"];
cv.img3.image = [UIImage imageNamed:@"jg3.png"];
cv.img4.image = [UIImage imageNamed:@"jg4.png"];
cv.img5.image = [UIImage imageNamed:@"jg5.png"];
cv.img6.image = [UIImage imageNamed:@"jg6.png"];
cv.lb1.text = @"需求管理";
cv.lb2.text = @"我的消息";
cv.lb3.text = @"通知公告";
cv.lb4.text = @"政策指南";
cv.lb5.text = @"企业搜索";
cv.lb6.text = @"我的工具";
}else if([type isEqualToString:@"qy"]){
cv.img1.image = [UIImage imageNamed:@"qy1.png"];
cv.img2.image = [UIImage imageNamed:@"qy2.png"];
cv.img3.image = [UIImage imageNamed:@"qy3.png"];
cv.img4.image = [UIImage imageNamed:@"qy4.png"];
cv.img5.image = [UIImage imageNamed:@"qy5.png"];
cv.img6.image = [UIImage imageNamed:@"qy6.png"];
cv.lb1.text = @"需求发布";
cv.lb2.text = @"政策指南";
cv.lb3.text = @"通知公告";
cv.lb4.text = @"我的消息";
cv.lb5.text = @"我的服务";
cv.lb6.text = @"我的工具";
}
return cv;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
- (void)dealloc {
[_img1 release];
[_img2 release];
[_img3 release];
[_img4 release];
[_img5 release];
[_img6 release];
[_lb1 release];
[_lb2 release];
[_lb3 release];
[_lb4 release];
[_lb5 release];
[_lb6 release];
[_contentview release];
[_tv1 release];
[_tv2 release];
[_tv3 release];
[_tv5 release];
[_tv6 release];
[_tv4 release];
[super dealloc];
}
@end
CenterView.xib
绑定文件就不讲了,主要注意绑定的时候custom class要选择CenterView。