user1060500
user1060500

Reputation: 1535

ViewDidLoad not being called with my custom UIViewController

I have created a custom UIViewController class that creates a ScrollView at runtime that it loads into the view. See code here in the constructor of my custom UIViewController.

        initControl(id, canEdit);

        _controllers = new NSMutableArray(0); //required to keep view controllers around

        _scrollView = new UIScrollView();
        _scrollView.BackgroundColor = UIColor.Green;
        this.View = _scrollView; 

ViewDidAppear and ViewWillAppear are called normally.

ViewDidLoad is not called which I am not sure why as the view is showing up on the screen just fine.

Any ideas?

Upvotes: 3

Views: 1179

Answers (2)

Girish
Girish

Reputation: 4712

ViewDidLoad is called when you are allocating the view. So if you are allocating the view once & only adding every time using addSubview then it called first time only. If you want to called it every time when you are adding it, then you needs to allocate it every time. Also handle memory management by releasing the view before allocating it, if it is already allocated. Another way is to create a method which contains the operations which you wants to perform & called it after addSubview. It may solves your problem, if you have any doubt then feel free to ask me.

Upvotes: 0

CReaTuS
CReaTuS

Reputation: 2585

The viewDidLoad method is being called when accessing self.view

Examples:

1)

- (id) init {
      self = [super init];
      if (self)
      {
          ...
          [self.view addSubview: self.toolbar];
      }
 }

2)

viewContrl = [[MyViewController alloc] init];
viewContrl.view = webTopView;

3)

viewContrl = [[MyViewController alloc] init];
[viewContrl.view addSubview: webTopView];

Upvotes: 2

Related Questions