Why do both init functions get called

If I have a class that is setup like this to customize a UIView.

@interface myView : UIView

  • Counting the number of lines in a UITextView, lines wrapped by frame size
  • Detect which app is currently running on iOS using sysctl
  • Objective-C: Play Youtube Video on App
  • Scheduled Local Notifications showing up when toggled ON for time period when toggled OFF
  • Auto-sizing UICollectionView headers
  • How I auto size a UIScrollView to fit the content
  • - (id)init {
        self = [super init];
            [self foo];
        return self;
    - (id)initWithFrame:(CGRect)frame
        self = [super initWithFrame:frame];
        if (self) {
            // Initialization code
            [self foo];
        return self;
    -(void) foo{
       //Build UIView here 

    How come foo is called twice whether I use

    myView *aView = [[myView alloc]init]]; 


    myView *aView = [[myView alloc]initWithFram:aFrame]; 

    Solutions Collect From Internet About “Why do both init functions get called”

    UIView init calls UIView initWithFrame:. Since you override both, calling your init method results in your initWithFrame: method being called:

    In other words: your init calls UIView init. UIView init calls initWithFrame:. Since you override initWithFrame:, your initWithFrame: is called which in turn calls UIView initWithFrame:.

    The solution, since your initWithFrame: will always be called, is to remove the call to foo from your init method.