您的位置:首页 > 产品设计 > UI/UE

加载自定义xib UIView的方法和加载纯代码 UIView代码的区别

2015-11-09 10:49 387 查看
一.使用加载xib 时候

+ (instancetype)phoneCounselSelecteProductView{

    return [[[NSBundlemainBundle]
loadNibNamed:NSStringFromClass([selfclass])
owner:niloptions:nil]firstObject];

}//nib 就是xib 的意思哦。在file'owner 里是不需要设置 类名的。在View 中是需要设置的。

用上面这个类方法初始化类。

+ (instancetype)loadFromXib {

    return [[[NSBundlemainBundle]
loadNibNamed:@"WHMyWarmHeartCell"owner:niloptions:nil]lastObject];

}
对比发现:
[NSStringFromClass([self class]) ] 就是NSString 类名。

二.使用纯代码



[[PhoneConsultServiceInstructionViewalloc]
initViewWith:self.doctorWithProduct.itemDescription];

貌似是自定义了一个init 方法。

(当然也可以是[[PhonecConsultServiceInstructionView alloc] init];

-(instancetype) initViewWith :(NSArray *)array 

{

  self = [super init];

if(self)

{

//自定义View;

}

return self;

}

默认的init方法是什么样的呢?

就是return self ,而 alloc 就是分配内存。

引申:

对于NSString 和NSArray :

NSString * name = [NSString alloc];

NSLog(@"%p",name);

name = [name init];

NSLog(@"%p",name);

对于这两次的内存地址不一样是因为

NSString 的 init 方式如此定义:

-(id) init {

if (self = [super init])

{

//

}

return self;

}

在self = [super init]中如果不为nil,就重新分配内存空间,所以

两次内存地址不一样。

而对于NSObject 本身是基类,无法调用[super init];所以两次内存地址是一样的。

再引申:

alloc init 与 New 的区别

new 基本等同于alloc,init .只是new 不支持自定义的init方法,就如同上面所讲的一样。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: