Objective C: My custom -init method not getting called

后端 未结 1 1406
一个人的身影
一个人的身影 2021-01-18 08:46

I have a class that I\'m deriving from UIView, and I wanted to create a -init class for it like so:

- (id) init
{
    if (self = [super init]) {
        // m         


        
相关标签:
1条回答
  • 2021-01-18 08:56

    There are two designated initializers for UIViewController and UIView they are initWithCoder called from nib, and initWithFrame called from code. Init is not the designated initializer for those objects.

    If you want to cover 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;
    }
    
    0 讨论(0)
提交回复
热议问题