0

I have a programmatically created UIViewController named as "VC" and on top of that I need to load my existing UIViewController.

I used below code to do that, and it's working fine. I can see my existing UIViewController on "VC" but not detecting any of viewDidAppear or viewWillAppear in existing view controller.

I am getting data from viewDidAppear and viewWillAppear so all the time my existing view controller collection view is empty.

ExistingViewController* presObj= [self.storyboard instantiateViewControllerWithIdentifier:@"oad"];

[vc.view addSubview:presObj.view];
[self addChildViewController:presObj];
[presObj didMoveToParentViewController:self];
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Bangalore
  • 1,572
  • 4
  • 20
  • 50

3 Answers3

0

Have you initialize the storyboard class?

self.storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

so all the time my existing ViewController collection view is empty --> are you using collection view in your code?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • am doing above code in some other class where i dynamically created VC, then try to load existing ViewCOntrolling on top of that – Bangalore Jan 07 '16 at 06:26
0

First you need to add the addChildViewController. Then add that childviewcontroller view into your main view.

Just add the [self addChildViewController:presObj]; before adding to the view subview.

ExistingViewController* presObj= [self.storyboard instantiateViewControllerWithIdentifier:@"oad"];
[self addChildViewController:presObj];
[vc.view addSubview:presObj.view];
[presObj didMoveToParentViewController:self];
Ramesh_T
  • 1,251
  • 8
  • 19
  • Did you check your child view is adding you main view hierarchy. http://stackoverflow.com/a/20769399/3051458 – Ramesh_T Jan 07 '16 at 06:42
-2

Ok, If you are loading like this then your ViewDidLoad () of existing view controller will load, and you can put a condition (if needed) then you can call ViewDidAppear () ViewWillAppear() like below

 existingviewcontroller *obj=[[existingviewcontroller alloc]init];
        [obj viewWillAppear:YES];
        [obj viewDidAppear:YES];

This will work

  • 2
    No, don't do this. You should not explicitly call `viewWillAppear:` or other lifecycle methods. – rmaddy Jan 07 '16 at 06:55