3
votes

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?

2

2 Answers

2
votes

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];
0
votes

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.