Search code examples
objective-cmethodsself

Objective-C how to call a method after self is initialized inside the object file?


Is there any way to know when a custom object is finished with being initialized from inside the object's file? Or let me rephrase the question, why can't I call any method inside this method?

- (id)initWithCoder:(NSCoder *)coder {
    //NSLog(@"initWithCoder inside CustomObject (subclass of UIView)");
    self = [super initWithCoder:coder];
    if (self) {
        //... initialization here


        [self visibleEmptyButton]; //why does this method never get called?


    }
    return self;
}

EDIT:

-(void)viewDidLoad{



    NSLog(@"viewDidLoad inside CustomObject(subclass of UIView) is called"); //It never gets called
    [self viewDidLoad];
    //initialization here...


}

Solution

  • (If the class you are init-ing is a subclass of UIViewController) Changing and setting things in the screen should be done after the view is loaded. Try doing it in this method:

    - (void)viewDidLoad {
        [super viewDidLoad];
    
        [self visibleEmptyButton];
        //Do the additional view altering here
    }
    

    If this method doesn't exist yet you can just add it to the .m file (no need to add it to the .h file).