首页 > 其他 > 详细

init 和 initWithFrame 区别

时间:2016-03-20 21:22:35      阅读:250      评论:0      收藏:0      [点我收藏+]

当我们去自定义一些控件时 可以重写:

- (instancetype)init;

也可以去重写:

(instancetype)initWithFrame:(CGRect)frame

 

下面关于这两个的差异:

#import "BFView.h"

@implementation BFView

- (instancetype)init{
    
   self = [super init];
    
    NSLog(@"%@",NSStringFromCGRect(self.frame));
    
    return self;
    
}

- (instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    
    NSLog(@"%@",NSStringFromCGRect(self.frame));
    
    return self;
}
#import "ViewController.h"
#import "BFView.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    
    
    [super viewDidLoad];
    
    BFView *bfv = [[BFView alloc]init];
    
    BFView *bfv2 = [[BFView alloc]initWithFrame:CGRectMake(3, 4, 5, 6)];

}



@end
技术分享

综上所述:

实现init这个方法咱们会先去调用initWithFarme这个方法 并且frame的所有值都为0;

实现iniwithFrame 这个方法 在初始化的时候 就已经把frame设置好了 并且不会去调用init这个方法

 

init 和 initWithFrame 区别

原文:http://www.cnblogs.com/zhubaofeng/p/5299557.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!