简体   繁体   English

在超类上调用-initWithFrame:

[英]Calling -initWithFrame: on super class

Can someone help me understanding as why do we call an initialization method on super first before initializing. 有人可以帮助我理解为什么我们在初始化之前先在super上调用初始化方法。 I came across a piece of code in a subclass of UIView and wanted to know that here myMethod is always getting called that means I am not getting the frame set in UIView , then why are we doing this and using an if condition. 我在UIView的子类中遇到了一段代码,想知道在这里总是被调用myMethod,这意味着我没有在UIView获得框架集,那么为什么我们要这样做并使用if条件。

self = [super initWithFrame:CGRectMake(0, 0, 20, 100)];
if(self != nil) {
  [self myMethod:data];
  self.backgroundColor = [UIColor clearColor];
}
return self;

Let's say I have a UIView subclass called SpinningView . 假设我有一个名为SpinningViewUIView子类。 The code to create spinningView would look like this: 创建spinningView的代码如下所示:

SpinningView *spinner = [[SpinningView alloc] initWithFrame:CGRectMake(0.0, 0.0, 20.0, 20.0)]

Now let's take a look at the implementation of SpinningView's -initWithFrame: method 现在让我们看一下SpinningView的-initWithFrame:方法的实现

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];

    if (self)
    {
        self.backgroundColor = [UIColor clearColor];
    }

    return self;
}

The first line is simple assignment. 第一行是简单的分配。 We're assigning ourselves to the result of UIView 's implementation of -initWithFrame: 我们将自己分配给UIView的实现结果-initWithFrame:

We use the if statement to see if self is even valid. 我们使用if语句查看self是否有效。 If it's not we just return it. 如果不是,我们就将其退回。 If it is, we configure it. 如果是这样,我们对其进行配置。

This is simply calling the constructor of the super class (in this case UIView). 这只是调用超类的构造函数(在本例中为UIView)。

You need to call UIView's constructor to make sure that all the variables you don't see from your subclass is set up properly, and that's what you do with [super init] (or in this case initWithFrame:). 您需要调用UIView的构造函数,以确保正确设置了您从子类中看不到的所有变量,而这正是使用[super init] (或本例中的initWithFrame :)所做的。

There are a few other reasons why it looks like this. 看起来像这样还有其他一些原因。

First of all [super init] might return nil. 首先, [super init]可能返回nil。 Perhaps something went wrong initializing things in the code of the superclass. 可能在初始化超类的代码时出错。 The check for self != nil makes sure you don't use the object when something is already wrong with the object. self != nil的检查可确保在对象已经存在问题时不使用该对象。 It might even have been released by the super constructor. 它甚至可能已由超级构造函数发布。

In other cases the super constructor might actually return a different object altogether. 在其他情况下,超级构造函数实际上可能完全返回另一个对象。 Examples of this happening is with class clusters or when you implement a singleton. 这种情况的例子是在类集群中或在实现单例时。

To summarize: 总结一下:

  1. Always call the designated constructor (ie init-method). 始终调用指定的构造函数(即init-method)。
  2. Always use the standard construct of if ((self = [super init])) { /* own init */ } return self; 始终使用if ((self = [super init])) { /* own init */ } return self;的标准构造if ((self = [super init])) { /* own init */ } return self;
  3. Sometimes it looks different, but only for special reasons. 有时看起来有所不同,但仅出于特殊原因。 If in doubt, always use (2). 如有疑问,请始终使用(2)。

Apple's documentation has a lot more info on this is you're interested. Apple的文档中对此有更多信息,您对此感兴趣。

Also, when overriding constructors like this, remember that there might be more than one constructor. 另外,当像这样重写构造函数时,请记住可能存在多个构造函数。 For instance, if you add the view using Interface Builder, that view will be initialized using "initWithCoder:" instead. 例如,如果使用Interface Builder添加视图,则将使用“ initWithCoder:”来初始化该视图。 All constructors begin with "init" though. 但是,所有构造函数都以“ init”开头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM