纳米卡一直是升级,纳米盒第4季什么时候出可以抢

2616人阅读
ios开发进阶篇(114)
经常使用Xib制作自定义的UITableViewCell, 有两个神器我爱不释手, 一个是Xib另外一个是Autolayout. 于是想把一些UIView也通过Xib制作成可以复用的View, 这样可以灵活在StoryBorad里面使用.
1.先新建一个自定义的XibUIView继承UIView, 然后再新建一个Xib,然后在File's Owner里面写入你的自定义的XibUIView.
下午8.56.41.png
2.然后在把里面的对象映射到.h或者.m文件里面,记住是通过File's Owner映射带代码里面.
#import &UIKit/UIKit.h&
@class UIButtonB
@interface XibUIView : UIView
@property (strong, nonatomic) IBOutlet UIView *
@property (weak, nonatomic) IBOutlet UILabel *rateInfoL
@property (weak, nonatomic) IBOutlet UIButtonBlock *yesB
@property (weak, nonatomic) IBOutlet UIButtonBlock *noB
下午9.00.26.png
3.最后很重要的一点就是就是重写View里面的一个方法,- (instancetype) initWithCoder:(NSCoder
*)aDecoder, 当Storyboard从Xib里面初始化视图的时候只会调用这个方法, 所以我们重写这个方法就可以了.
#import &XibUIView.h&
#import &UIButtonBlock.h&
@implementation XibUIView
- (instancetype) initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
[[NSBundle mainBundle] loadNibNamed:@&XibUIView& owner:self options:nil];
self.frame = self.view.
[self addSubview:self.view];
[self.yesButton rateWhiteWordGreenBackGround];
[self.noButton rateGreenWordWhiteBackGround];
4.然后在StoryBoard里面把View的类名该成XibUIView,只会运行你就会发现你的Xib出现在设备里面了.
下午9.06.04.png
一定要要在[self addSubview:self.view];&初始化self.frame不然添加视图的时候会有问题.
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:303976次
积分:4850
积分:4850
排名:第5666名
原创:162篇
转载:167篇
评论:24条
欢迎大家加入QQ群: 一起来分享交流技术
(1)(2)(1)(1)(1)(1)(1)(2)(2)(4)(1)(2)(2)(2)(9)(3)(4)(5)(7)(32)(28)(11)(20)(40)(4)(145)(4)iOS&-&使用xib文件封装一个自定义view
使用xib文件封装一个自定义view
使用xib封装一个自定义view的步骤
新建一个继承UIView的自定义view,假设类名叫做(MJAppView)
新建一个MJAppView.xib文件来描述MJAppView内部的结构
修改UIView的类型为MJAppView真是类型
将内部的子控件跟MJAppView进行属性连线
MJAppView提供一个模型属性
重写模型属性的set方法,因为在set方法中可以拿到外界传递的模型数据
把模型数据拆开,分别设置数据到对应的子控件中
补充:提供一个创建MJAppView的类方法,将读取xib文件的代码屏蔽起来
---------------LeeViewController.h---------------
"LeeViewController.h"
"appModel.h"
"LeeView.h"
&&for (int index = 0; index & self.apps.count; index++) {
& & //获取每个appView的位置
CGFloat locationOfViewX =
distanceOfSideX + (sizeOfViewX +
distanceOfSideX)*(index%viewNumX);
CGFloat locationOfViewY =
30 +(sizeOfViewY +
distanceOfSideY)*(index%viewNumY);
& & //创建一个appView
LeeView *xibView =
[LeeView LeeView];
& & //将创建出的appView添加到父view中
[self.view addSubview:xibView];
& & //设置appView的位置
xibView.frame = CGRectMake(locationOfViewX,
locationOfViewY, sizeOfViewX, sizeOfViewY);
//设置数据--通过apps方法拿到储存着数组的数据模型并传给appview,在appView的内部将传入进来对数据赋予appView中的控件属性(appView中也相应定义了一个和数据模型类型相同的属性)
xibView.app = self.apps[index];
//字典转模型
- (NSArray *)apps
(_apps == nil) {
NSBundle *bundle =
[NSBundle mainBundle];
NSString *path = [bundle
pathForResource:@"app.plist" ofType:nil];
NSArray *dicArray =
[NSArray arrayWithContentsOfFile:path];
NSMutableArray *appArray =
[[NSMutableArray alloc] init];
for (NSDictionary *dict in dicArray) {
& & appModel *app = [[appModel alloc] initWithDict:dict];
& & [appArray addObject:app];
---------------appModel.h---------------
@interface appModel : NSObject
(nonatomic, copy)
NSString *
(nonatomic, copy)
NSString *
- (instancetype) initWithDict:(NSDictionary *)
+ (instancetype) modelWithDict:(NSDictionary *)
---------------appModel.m---------------
"appModel.h"
@implementation appModel
- (instancetype)initWithDict:(NSDictionary *)dict
(self = [super init]) {
_name = dict[@"name"];
_icon = dict[@"icon"];
return self;
+ (instancetype)modelWithDict:(NSDictionary *)dict
[[self alloc]initWithDict:dict];
---------------LeeView.h---------------
@interface LeeView : UIView
(nonatomic, strong) appModel *
+ (LeeView *) LeeV
---------------LeeView.m---------------
"LeeView.h"
"appModel.h"
@interface
(weak, nonatomic) IBOutlet UIImageView *iconV
(weak, nonatomic) IBOutlet UILabel *nameV
@implementation LeeView
- (void)setApp:(appModel *)app
& self.iconView.image = [UIImage imageNamed:app.icon];
& self.nameView.text = app.name;
+ (LeeView *)LeeView
//用bundle读取xib
& NSBundle
*bundle = [NSBundle
mainBundle];
*xibArray = [bundle loadNibNamed:@"LeeView" owner:nil options:nil];
//根据xib内部的界面描述创建出一个appView
[xibArray lastObject];
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。

我要回帖

更多关于 纳米盒第4季什么时候出 的文章

 

随机推荐