Goal C: My custom -init method does not receive a call

I have a class that I get from UIView, and I wanted to create a -init class for it as follows:

- (id) init { if (self = [super init]) { // my initializations here } return self; } 

Unfortunately, I know this is not caused. This is how I define it in my .h:

 -(id)init; 

Does anyone know why they don't call him?

ADDITION:

I found that when initializing in initWithCoder it works fine. I added initWithFrame, but it was not called.

This is for the object that I indicated in IB.

+6
source share
1 answer

There are two designated initializers for UIViewController and UIView , which they are called initWithCoder from nib and initWithFrame , called from code. Init not a designated initializer for these objects.

If you want to bypass both bases, you can do something like this:

 -(void)initializationCodeMethod{ <#Initialization Code Here#> } -(id)initWithFrame:(CGRect)frame{ if ((self = [super initWithFrame:frame])){ [self initializationCodeMethod]; } return self; } -(id)initWithCoder:(NSCoder *)aDecoder{ if ((self = [super initWithCoder:aDecoder])){ [self initializationCodeMethod]; } return self; } 
+13
source

Source: https://habr.com/ru/post/901916/


All Articles