美文网首页
OC中init、initWithFrame、initWithCo

OC中init、initWithFrame、initWithCo

作者: colacola | 来源:发表于2017-11-12 18:39 被阅读129次

假定我们需要自定义一个View,命名为MyView,当我们需要使用它当使用,一般情况下我们可以手写代码创建或者从Xib、Storyboard中创建。

@implementation MyView

- (instancetype)init
{
    self = [super init]; // call the designated initializer
    if (self) {
        // Custom initialization
        NSLog(@"init");
    }
    return self;
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self commonInit];
        NSLog(@"initWithFrame");
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        [self commonInit];
        NSLog(@"initWithCoder");
    }
    return self;
}

- (instancetype)awakeFromNib {
    NSLog(@"awakeFromNib");
    [super awakeFromNib];
}

- (void)commonInit
{
    // do any initialization that's common to both -initWithFrame:
    // and -initWithCoder: in this method
}

一、不同创建方式的调用情况

1、手写init创建

MyView *myView =[ [MyView alloc] init];
当使用该方法创建时,init和initWithFrame方法都调用了。

2、手写initWithFrame创建

MyView *myView =[ [MyView alloc] initWithFrame:CGRectZero];
当使用该方法创建时,initWithFrame调用了。

3、使用Xib、Storyboard创建

initWithCoder、awakeFromNib会调用。

二、几种方法的区别

1、init 和 initWithFrame:

在使用[[Class alloc]init];方法创建对象时,会先调用的是initWithFrame方法,然后再调用init方法。
关于init


58b65c25d3dc0.jpg

可知:
1)init是让子类实现初始化一个新对象的方法
2)NSObject类的init没有初始化操作,只是简单的返回了一个self指针in 在UIView 调用 [super init] i类似于调用 [super initWithFrame:CGRectZero]的效果。所以手写创建时,只需要在initWithFrame方法中写初始化设置的代码,可以确保代码被执行。不需要在init和initWithFrame方法中各写一次。

2、awakeFromNib: 和 initWithCoder:

awakeFromNib: 从xib或者storyboard加载完毕就会调用
initWithCoder: 只要对象是从文件解析来的,就会调用
同时存在会先调用initWithCoder:

3、initWithCoder: 和 initWithFrame:

initWithCoder: 和 initWithFrame:都是指定初始化方法(designated initializer)。
initWithCoder:使用文件加载的对象调用(如从xib或stroyboard中创建)
initWithFrame:使用代码加载的对象调用(使用纯代码创建)
注意:所以为了同时兼顾从文件和从代码解析的对象初始化,要同时在initWithCoder: 和 initWithFrame: 中进行初始化。

4、awakeFromNib

当.nib文件被加载的时候,会发送一个awakeFromNib的消息到.nib文件中的每个对象,每个对象都可以定义自己的awakeFromNib函数来响应这个消息,执行一些必要的操作。也就是说通过nib文件创建view对象时执行awakeFromNib。

最后

在初始化控件的代码时,由于其他人用手写或是从xib或stroyboard中创建不得而知,为安全起见,可以将初始化设置的代码分别在initWithFrame和initWithCoder中都调用一下,以保证初始化设置的代码都能被调用。

参考文献:
https://github.com/oa414/objc-zen-book-cn/#designated-initializer
http://www.cnblogs.com/yajunLi/p/6344023.html?hmsr=toutiao.io
https://stackoverflow.com/questions/7226239/objective-c-custom-view-and-implementing-init-method
http://www.iosugar.com/2017/03/01/UIView-init-and-initWithFrame-call-relationship/

相关文章

  • OC中init、initWithFrame、initWithCo

    假定我们需要自定义一个View,命名为MyView,当我们需要使用它当使用,一般情况下我们可以手写代码创建或者从X...

  • init initWithFrame问题

    MyView *myView =[[MyView alloc]init]; 代码调用过程如下: 动态查找到 MyV...

  • init还是initWithFrame?

    当年学iOS的时候,有好多的知识点是知其然不知其所以然,随着时间的推移和开发的深入,很多当时不懂或者不是很懂的知识...

  • View的初始化函数

    ShopView类 init 方法内部会调用initWithFrame:方法 打印结果 initWithFrame...

  • 控制器生命周期

    1 init函数(init;initWithFrame;initWithCoder;等)--初始化 2 awake...

  • iOS drawRect、initWithFrame、init、

    前言最近在研究关于Quart2D方面的知识,通过在drawRect方法内可以对一个视图进行绘制并呈现出我们所想要的...

  • init:、initWithFrame: 调用选择

    前段时间准备面试时,突然被问到一个问题:init: 和 initWithFrame: 方法应该调用哪个?为什么? ...

  • 字符串转类

    ``` oc中 [NSClassFromString(@"***")alloc]init; swift varcl...

  • UI-2

    172.简述视图控制器的生命周期。 1)init函数(init;initWithFrame;initWithCod...

  • iOS 自定义View 的总结注意

    参考连接 搜先自定义一个view 然后分别用 init 和 initWithFrame 来实现 init init...

网友评论

      本文标题:OC中init、initWithFrame、initWithCo

      本文链接:https://www.haomeiwen.com/subject/mhkomxtx.html