1

I have a main UIViewController and named AddInformationController. In this mainViewController I have two UIContainerView. Each of them embed with a UITableViewController. The table view controllers are named InformationSegmentController and ResultSegmentController.

I put this in AddInformationController:

InformationSegmentController *isc = [self.storyboard instantiateViewControllerWithIdentfier:@"InformationSegmentController"];
ResultSegmentController *rsc = [self.storyboard instantiateViewControllerWithIdentifier: @"ResultSegmentController"];
[self addChildViewController: isc];
[self addChildViewController: rsc];

In either the InformationSegmentController class or ResultSegmentController class, I tried to Log this: NSLog(@"%@",self.parentViewController); But unfortunately, it is a nil. How can I get the parentViewController correctly? Do I have to use the prepareForSegue?

// Question Update:

I found that I don't have to call addChildViewController method again if I create those childViewControllers directly in my storyboard.

I have tried to NSLog(@"%@", self.childViewControllers) in my AddInformationController and the result contains InformationSegmentController class and ResultSegmentController class.

But the problem is when I call NSLog(@"%@", self.parentViewController) on both childViewContorllers in viewDidLoad, the results is nil.

Johnny Cheuk
  • 237
  • 2
  • 15
  • Obviously it all depends on _when_ you do that logging and _who_ is `self`. Perhaps the `self` here is not the same instance you added as a child. – matt Mar 31 '16 at 03:51
  • I have put the `NSLog(@"%@", self.parentViewController)` in the `viewDidLoad` method in the InformationSegmentController class – Johnny Cheuk Mar 31 '16 at 03:55
  • Now I understand. The childViewController's viewDidLoad calls earlier than the parentViewController. So I probably NSLog it before the parentViewController even add the childViewController. – Johnny Cheuk Mar 31 '16 at 04:10

2 Answers2

1

You have to try like this

//add childview
    [self addChildViewController:aVC];
    [self.view addSubview:aVC.view];
    [aVC didMoveToParentViewController:self];

    //remove
    [self removeFromParentViewController];
    [self didMoveToParentViewController:nil];
    [self.view removeFromSuperview];

Hope it Helps you.

OR
another way to get PerentViewController in ChildViewControler is Make @property (strong, nonatomic) AddInformationController *ParentVC; in Your ChildViewController.h and assign viewController from your ParentViewController Like

InformationSegmentController *isc = [self.storyboard instantiateViewControllerWithIdentfier:@"InformationSegmentController"];
isc.ParentVC = self;
[self addChildViewController: isc];
Vvk
  • 4,031
  • 29
  • 51
  • 1
    If you put `@property (strong, nonatomic) AddInformationController * ParentVC` in the InformationSegmentController.h inside the interface, when you compile it, there will be an error saying that `Unknown Type name AddInformationController`. I'm 101% sure I have import that header. – Johnny Cheuk Mar 31 '16 at 05:42
  • 1
    And I found out that this is much more about the circular issue. Since the childViewController must load before the parentViewController, if you try to import the parentViewController in the childViewController, the childViewController doesn't know what is parentViewController because it doesn't not exist yet. So I just use `@class` instead of `#import` and the problem was fixed. – Johnny Cheuk Mar 31 '16 at 05:49
  • as per my understanding this is Cycle issue of viewController, but also you have imported Properly. may be it helps you http://stackoverflow.com/questions/7897268/xcode-unknown-type-name **OR** try to replace `(weak, nonatomic)` . also i will find another way for you. – Vvk Mar 31 '16 at 05:52
  • No no no, the cycle issue was already fixed :) Thanks for the help – Johnny Cheuk Mar 31 '16 at 05:54
0

Give identifiers to both the segues to your container views(suppose "InformationSegmentControllerSegue" and "ResultSegmentControllerSegue" ,namely) and use prepareForSegue in your AddInformationController like this:

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
        if ([[segue identifier] isEqualToString:@"InformationSegmentControllerSegue"]) {
            InformationSegmentController *container1 = segue.destinationViewController;
        }
else if([[segue identifier] isEqualToString:@"ResultSegmentControllerSegue"]) {
            ResultSegmentController *container2 = segue.destinationViewController;
        }
    }

Hope this helps you.

iOS Geek
  • 4,825
  • 1
  • 9
  • 30