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.)

4

2 回答 2

2

您可以在父视图控制器中使用“prepareForSegue”将 self 传递给容器视图,如下所示:

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

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

您在 ContainerViewController 类型的 ParentViewController 中创建合成属性并将该属性设置为 self。

这是您应该在情节提要中看到的内容: 在此处输入图像描述

于 2015-05-04T22:28:58.027 回答
0

MainViewController - 容器视图

您在容器视图中创建委托,主视图将从该委托实现。

你可以试试这个: http: //www.infragistics.com/community/blogs/torrey-betts/archive/2014/05/29/passing-data-between-view-controllers-ios-obj-c.aspx

于 2015-05-05T04:08:28.040 回答