1

I have a mainViewController with a container view in it. I'm trying to access theMainViewController from the container view.

Here is my code:

self.theMainViewController = (theMainViewController *)self.parentViewController;

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:idx inSection:0];
[self.theMainViewController .tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionBottom];

This doesn't work. self.theMainViewController gives a nil value when I make an nslog of it. I then replaced:

self.parentViewController

to:

self presentingViewController

and it gave me the same results. How can I access the mainViewController from the container view's class?

Update

This is my setup: Static table view inside UIViewController [Xcode 5] (I can't add images, so the image posted in that answer, is the same as my setup.)

Community
  • 1
  • 1
Jessica
  • 9,379
  • 14
  • 65
  • 136
  • Show how you setup the container view controller and the child view controllers. – rmaddy May 04 '15 at 22:16
  • @Jessica you may want to redefine your view controllers hierarchically. Here is great [article](http://www.objc.io/issue-1/containment-view-controller.html) describing how to do that. – Ozgur Vatansever May 04 '15 at 23:38

2 Answers2

2

You can use "prepareForSegue" in the parent view controller to pass self to the container view like so:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
     if ([[segue identifier] isEqualToString:@"containerview_myIdentifier"]) {

         ContainerViewController *vc = [segue destinationViewController];
         [vc setReferenceToParentVC:self];
    }
}

Where you create a synthesized property in ContainerViewController of type ParentViewController and set that property to self.

Here is what you should see in storyboard: enter image description here

Cole
  • 2,641
  • 1
  • 16
  • 34
  • What type of object is `ReferenceToParentVC`? – Jessica May 04 '15 at 22:43
  • @Jessica It should be "ParentViewController" for example, one of mine is as follows `@property (nonatomic) PagesViewController *referenceToRootViewController; ` Just be sure to declare the ParentViewController header in the head of the container view controller like this `#import "PagesViewController.h"` – Cole May 05 '15 at 00:18
  • Oh that's my bad, I forgot this will create a circular dependency loop. So instead declare the `pacesViewController` as `@class pacesViewController;` and then declare it regularly with `#import` in your .m – Cole May 05 '15 at 01:07
  • So should I do `[vc setReferenceToParentVC:self];`? – Jessica May 05 '15 at 01:32
  • Yes. That's fine as it sets the variable `refereceToParentVC` to the instance of the `ParentViewController` that holds the container view. – Cole May 05 '15 at 01:37
0

MainViewController - ContainerView

You create delegate in containerview and mainview will implement from this delegate.

You can try this: http://www.infragistics.com/community/blogs/torrey-betts/archive/2014/05/29/passing-data-between-view-controllers-ios-obj-c.aspx

Binladenit Tran
  • 121
  • 1
  • 1
  • 7