Is there an init method in iOS that is always called?

Is there a method that is always called in Cocoa? Many classes have initor initWith, but even worse, they can be loaded from the tip or something else. I do not want to have to scratch and find how this happens in this case. I just want to set some initial variables and other things, and I need a method for a subclass that I can depend on, regardless of whether they are UIView, UIViewControlleror UITableViewCellso on.

+5
source share
3 answers

There is no such method. initcomes from NSObject, so every object can use it, and subclasses define their own initialization methods. UIView, for example, defines initWithFrame:and, in addition, from the protocols there are init methods, such as NSCodingwhich defines initWithCoder:. This is the dynamic nature of objective-C, everything can be expanded at any time. That being said, there are several models. UIViewControlleralmost always accepts initWithNibName:bundle:, but UIViewalmost always accepts initWithFrame:or initWithCoder:. What I am doing is making an internal initialization method, and just calling other calls.

- (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];
    }
}
+9
source

100% , , , . , , - , , ( , , , , ...):

-didMoveToSuperview()

:

, . . , .

0

.

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

. , . ( , ).

Btw, init - . NSObject, init .

-1

All Articles