skip to Main Content

my didAddSubview is not being called.

This is what I am doing.

In my viewController

- (void)viewDidLoad {
    [super viewDidLoad];
  SomeView *view = [[SomeView alloc] initWithFrame:CGRectMake(0.0, 0.0,    CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame))];
  [self.view addSubview:view];
}

Where my SomeView is a UI view (.h file)

#import <UIKit/UIKit.h>

@interface DyteMeetingView : UIView
@end

in which I have didAddSubview like this

- (void)didAddSubview:(UIView *)subview{
    [super didAddSubview:subview];
    [self someFunc];
}

I placed debugger in didAddSubview but the debugger never reaches here and hence [self someFunc]; is also not called.

Since didAddSubview is a lifecycle method for UI view, I am also not calling it from anywhere.

Can someone help me in figure out the concept I missed out on/what I could be doing wrong?

PS: I don’t have any xib file for UIView.

PS: Intentionally added swift tag as well since there isn’t large chunk of code and I am confident that swift developers would be able to understand the question/code and might be able to help me out.

2

Answers


  1. didAddSubview in SomeView will be called when a sub view is added to an instance of SomeView, but that isn’t what you are doing here. You are adding a SomeView as a subview of another view.

    You want didMoveToSuperview; in this method you can use the superview property of self to identify the view that it moved to.

    Login or Signup to reply.
  2. The didAddSubview will be called in viewController.view, instead of your SomeView, because you using viewController.view.addSubview(subview)

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search