使用Swift在UIPageViewController子视图之间传递数据

时间:2016-04-05 06:51:43

标签: swift delegates uipageviewcontroller

我似乎找不到直接困境的具体答案。

我有UIPageViewController以编程方式加载6个子UIView个场景。它们托管了“添加”元素功能的各个阶段。目前,PageViewController类将每个子视图添加到一个数组中,并在需要时将它们实例化为:

storyboard?.instantiateViewControllerWithIdentifier("editViewController")

自然代表设置为在每个子场景之间滑动,因此不会运行“prepareForSegue”功能。

当页面滑动时,会实例化具有不同标识符的新场景。

从我的阅读中,我现在正在尝试设置一个委托,该委托接受一个字典的实例,该代理读取/存储该过程的每个阶段(不同的子UIPageView场景)的输入并更新字典

我使用“viewWillDissapear”和“viewWillAppear”方法来帮助设置传递数据到委托中。

不幸的是我遇到了问题,由于我缺乏经验,而且对这个具体问题没什么了解,我需要帮助!

主要问题:

我可以在UIPageViewController课程中设置一个数据代理,该代理可以与儿童UIViews进行“对话”或访问吗?

OR

有没有人知道将我的词典从孩子传给孩子的好方法????

3 个答案:

答案 0 :(得分:7)

您可以从child-> parent-> child传递数据,而不是从child->子项传递数据。

首先,当您使用 storyboard?.instantiateViewControllerWithIdentifier(" editViewController")在PageViewController中实例化每个子项时,请保存对它的引用。

类似

var firstViewController: EditViewController?
...
firstViewController = storyboard?.instantiateViewControllerWithIdentifier("editViewController") as EditViewController

接下来,使用 self.parentViewController 在每个子ViewController中获取一个引用到PageViewController。

var myPageViewController: PageViewContrller (or whatever yours is called) 
....
myPageViewController = self.parentViewController() as PageViewController (or whatever yours is called)

现在,使用这两个引用,您可以调用PageViewController和子节点中的函数来回传递数据。

答案 1 :(得分:2)

您应该查看UIPageViewController委托here

在视图控制器之间转换之前和之后,您将获得这两种方法。

- pageViewController:willTransitionToViewControllers: // called before the transition starts
- pageViewController:didFinishAnimating:previousViewControllers:transitionCompleted:  // called after the transition is finished

答案 2 :(得分:0)

从PageViewController向其子节点发送数据非常容易,而不使用任何segues

override func viewDidLoad() {
    super.viewDidLoad()
      self.delegate=self
      self.dataSource=self
    let childViewController=storyboard?.instantiateViewController(withIdentifier: "some identifier of view") as! ChildViewController
    if let cid = challengeId{
        print("Page Challenge id \(cid)")
    childViewController.challengeId=cid
    }
相关问题