简体   繁体   English

在iOS中是否有一个始终被调用的init方法?

[英]Is there a init method in iOS that is always called?

Is there a method that is always called in Cocoa? 是否有一个总是在Cocoa中调用的方法? Many classes have init or initWith , but even worse they can be loaded from a nib or something. 许多类都有initinitWith ,但更糟糕的是它们可以从nib或其他东西加载。 I don't want to have to scrape around and find how it does this in this case. 在这种情况下,我不想在周围找到它是如何做到的。 I just want to set some initial variables and other things, and I want a method to subclass that I can depend on no matter if it's a UIView , UIViewController or UITableViewCell etc. 我只是想设置一些初始变量和其他东西,我想要一个子类的方法,我可以依赖它,无论它是UIViewUIViewController还是UITableViewCell等。

No there is not such a method. 不,没有这样的方法。 init comes from NSObject so every object can use it, and as well subclasses define their own initialization methods. init来自NSObject因此每个对象都可以使用它,并且子类定义它们自己的初始化方法。 UIView , for example, defines initWithFrame: and furthermore there are init methods from protocols, such as NSCoding which defines initWithCoder: . 例如, UIView定义了initWithFrame:此外还有来自协议的init方法,例如定义initWithCoder: NSCoding initWithCoder: . This is the dynamic nature of objective-C, anything can be extended at any time. 这是Objective-C的动态特性,任何东西都可以随时扩展。 That being said, there are some patterns. 话虽如此,有一些模式。 UIViewController almost always takes initWithNibName:bundle: and UIView almost always takes initWithFrame: or initWithCoder: . UIViewController几乎总是采用initWithNibName:bundle:UIView几乎总是采用initWithFrame:initWithCoder: . What I do is make an internal initialize method, and just have the other inits call it. 我所做的是制作一个内部初始化方法,让其他人调用它。

- (void)initialize
{
    //Do stuff
}

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

- (id)initWithCoder:(NSCoder *)aCoder
{
    self = [super initWithCoder:aCoder];
    if(self)
    {
        [self initialize];
    }
}

Not 100% sure that it is always called, but I am pretty sure that this is a viable option. 不是100%肯定它总是被调用,但我很确定这是一个可行的选择。 To be perfectly honest, I can't recall that I have ever seen this method used in practice and I usually shy away from using this method (I have absolutely no idea why, probably because it's just not the cleanest and most comprehensive method to achieve this...): 说实话,我不记得我曾经见过这种方法在实践中使用过,我常常回避使用这种方法(我完全不知道为什么,可能是因为它不是最干净,最全面的方法来实现这个...):

-didMoveToSuperview()

From documentation: 来自文档:

Tells the view that its superview changed. 告诉他们超级视图发生了变化。 The default implementation of this method does nothing. 此方法的默认实现不执行任何操作。 Subclasses can override it to perform additional actions whenever the superview changes. 每当superview更改时,子类都可以覆盖它以执行其他操作。

There's many ways you can write a custom initializer. 您可以通过多种方式编写自定义初始化程序。

- (id)initWithString:(NSString *)string {
    if((self == [super init])) {
        self.string = string;
    }
    return self;
}

That's just how I write my initializers in general. 这就是我一般编写初始化器的方式。 For example, the one above takes a string. 例如,上面的一个字符串。 (you don't have to pass strings if you don't want). (如果你不想要,你不必传递字符串)。

Btw, init is a method. 顺便说一句,init是一种方法。 According to the header for NSObject, init has a method implementation. 根据NSObject的头文件,init有一个方法实现。

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

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