How to retain the reference of [x]-number of viewControllers without declaration

时间:2017-06-20 12:33:59

标签: ios swift viewcontroller retain-cycle

I have a mainViewController in which I want to show a small gridlike formation of an undefined count of childViewControllers. The childViewControllers is created on the basis of data and therefore I can't know the count of them in advance. As it seems, I can only interact with f.ex. buttons in the childViewController if it is retained by the mainViewController (f.ex. in a declared variable) - so my question is, how do I retain the reference between the mainViewController and the [x]-number of childViewControllers without declaring them as individual variables?

I've tried to create parent variable on the childController and set it to the mainViewController, but this doesn't solve the problem - (the buttons in the childViewController is only functionable if I declare each childController individually..) I've tried to create an array of childControllers and adding them to that in the mainViewController, but also this doesn't work.

I'm looking not to use collectionView as there will be alot of customization and animations involved, so what is the correct method to retain these childViewControllers?

1 个答案:

答案 0 :(得分:0)

如果我理解你的问题:

UIViewController有一个属性ReferenceError: diff is not defined ,它是一个数组。

您可以阅读https://developer.apple.com/documentation/uikit/uiviewcontroller实施容器视图控制器部分,了解如何正确添加子视图控制器。

然后您可以使用数组下标来获取[x]子视图控制器,如下所示:

childViewControllers

父视图控制器中的myContainerViewController.childViewControllers[x].whateverButton ... // or let childX = myContainerViewController.childViewControllers[x] childX.doSomething() 数组将保留子视图控制器。

编辑:

如果您需要访问子类的属性,则必须将UIViewController向下转换为子类swift4 link, should be the same for swift3

你这样垂头丧气:

childViewControllers