在 UIPageViewController 中以编程方式转到下一页

Go to the next Page programmatically within a UIPageViewController

我有一个根ViewController,它基本上是我的页面ViewController,它实现了所有页面控制器协议功能。我已经使用正确显示当前页码的 pageControl 成功实现了整个页面ViewController。

在根 ViewController 中包含的第一页上,我有一个 viewController,其中有多个必须用 pickerView 填充的文本字段。我还创建了一个伴随选择器的 inputAccessory 工具栏。一旦所有字段都填满了文本,工具栏上的 barButton 就会切换到 Done。在这个 viewController 中点击 'Done' 按钮,我希望根 ViewController 转到下一页。

我首先尝试直接呈现我想转向的 viewController,但注意到该页面并未作为页面的一部分呈现ViewController(似乎很明显为什么它没有'现在不工作!)。所以我然后尝试实现一个协议,该协议将触发 rootViewController 翻到下一页。我已经完美地实现了一切,但没有设置委托。我似乎无法理解为什么没有设置它。这可能是因为我在 UIPageViewController 框架流程中可能遗漏了什么?我很难过。

最终在协议中,我试图在包含的 ViewController 和根 ViewController 之间进行通信。

这是我的源代码:

在根控制器中:

    import UIKit

    class rootPageViewController: UIViewController, UIPageViewControllerDataSource,
    UIPageViewControllerDelegate, tutorialPageTurnerDelegate {

        var tutorialOne = userInputViewController()

        @IBOutlet var pageLight: UIPageControl!

        var turn: UIPageViewController!
        let pages = ["userInput","tutorialOne","tutorialTwo","connectScreen"]
        var i: Int!

        override func viewDidLoad() {
            super.viewDidLoad()

            if let take = storyboard?.instantiateViewControllerWithIdentifier("pageView") {

                self.addChildViewController(take)
                self.view.addSubview(take.view)
                take.view.addSubview(pageLight)
                take.view.bringSubviewToFront(pageLight)

                turn = take as! UIPageViewController
                turn.dataSource = self
                turn.delegate = self

                // the delegate for the protocol I've implemented 
                tutorialOne.delegate = self

                turn.setViewControllers([viewControllerAtIndex(0)!], direction: UIPageViewControllerNavigationDirection.Forward, animated: true, completion: nil)
                turn.didMoveToParentViewController(self)

            }

        }

       .
       .
       ... pageviewcontroller protocol functions and misc code ...
       .
       .

 // Helper Function to return Index
    func viewControllerAtIndex(index: Int) -> UIViewController? {

        let vc = storyboard?.instantiateViewControllerWithIdentifier(pages[index])

// _________EDIT_________
    if index == 0 {
                var tutorialOne: userInputViewController = userInputViewController()
                tutorialOne.delegate = self
            }
        return vc

    }


// function required by the protocol
func saveActionCompleted() {

        print("Yes it does come here")
        turn.setViewControllers([viewControllerAtIndex(1)!], direction: UIPageViewControllerNavigationDirection.Forward, animated: true, completion: nil)
        turn.didMoveToParentViewController(self)

    }
...

现在这里是 viewcontroller 我创建协议的地方:

import UIKit
import CoreData

protocol tutorialPageTurnerDelegate {
    func saveActionCompleted()
}

class userInputViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate,
UITextFieldDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate {


    var delegate: tutorialPageTurnerDelegate?

override func viewDidLoad() {
        super.viewDidLoad()

 //Create toolbar
                let toolBar = UIToolbar()
                toolBar.barStyle = UIBarStyle.Default
                toolBar.translucent = true
                toolBar.tintColor = UIColor(red: 81/255, green: 45/255, blue: 168/255, alpha: 1)
                toolBar.sizeToFit()

let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Plain, target: self, action: "donePicker")
                let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
                toolBar.setItems([spaceButton, nextButton], animated: false)
                toolBar.userInteractionEnabled = true

....... more misc code ...

}

// The selector function for the toolbar from where I want to notify the root controller
 func donePicker(){

        self.view.endEditing(true)
        saveDataAction(self)

        if delegate != nil {
            delegate!.saveActionCompleted()
        } else {
            print("PageTurnerDelegate is nil")
        }
     }
...
} //End

我收到 PageTurnerDelegate 为零。我尝试了很多解决方法,但都徒劳无功。我确实觉得可能有更好的方法来做到这一点。欢迎提出任何建议或见解!

您需要设置将显示的视图控制器实例的delegate 属性:

func viewControllerAtIndex(index: Int) -> UIViewController? {

    let vc = storyboard?.instantiateViewControllerWithIdentifier(pages[index])
    if index == 0 {
            let userInputViewController =vc as! userInputViewController
            userInputViewController.delegate = self
    }
    return vc
}

此外,按照惯例,类 以大写字母开头,因此应该是 UserInputViewController