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

时间:2016-07-12 13:37:02

标签: ios swift protocols uipageviewcontroller uitoolbar

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

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

我首先尝试直接呈现我想转向的viewController,但注意到该页面并未作为pageViewController的一部分呈现(看起来很明显为什么它现在不起作用!)。所以我接着试图实现一个协议,它会触发rootViewController转到下一页。我已经完美地实现了所有功能,但代表没有被设置。我似乎无法理解为什么它没有被设定。这可能是因为我在UIPageViewController框架流程中可能遗漏了一些东西吗?我很难过。

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

这是我的源代码:

在rootController中:

    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为零。我尝试了一些解决方法,但都是徒劳的。我确实感觉可能有更好的方法来做到这一点。欢迎任何建议或见解!

1 个答案:

答案 0 :(得分:2)

您需要设置将显示的视图控制器实例的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