在 UIPageViewController 中以编程方式转到下一页 [英] Go to the next Page programmatically within a UIPageViewController

查看:37
本文介绍了在 UIPageViewController 中以编程方式转到下一页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a rootViewController which is basically my pageViewController which implements all the page controller protocol functions. I have already successfully implemented the whole pageViewController with a pageControl that correctly shows the current Page number.

在包含在 rootViewController 中的第一页上,我有一个 viewController,其中有多个 textFields 必须用一个 pickerView 填充.我还创建了一个 inputAccessory 工具栏,它伴随着选择器.一旦所有字段都填充了文本,工具栏上的 barButton 将切换为完成.在点击这个完成"按钮时,在这个 viewController 中,我希望 rootViewController 转到下一页.

On the first page contained within the rootViewController, I have a viewController in which there are multiple textFields which have to be filled in with a pickerView. I have also created an inputAccessory toolBar which accompanies the pickers. Once all the fields are filled with text, the barButton on the toolBar toggles to Done. On the tap of this 'Done' button, within this viewController, I want the rootViewController to go to the next page.

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

I first tried to directly present the viewController I wanted to turn to but noticed that the page was not presented as a part of the pageViewController(it seems pretty obvious why it didn't work now!). So I then tried to implement a protocol which would trigger the rootViewController to turn to the next page. I've implemented everything perfectly but the delegate is not being set. I can't seem to fathom why it isn't being set. Could this be because of something I may have missed in the UIPageViewController framework flow? I am stumped.

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

Ultimately in the protocol I am trying to communicate between a contained ViewController and the rootViewController.

这是我的源代码:

在根控制器中:

    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)

    }
...

现在这是我创建协议的视图控制器:

Now here is the viewcontroller where I've created the protocol:

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

I'm getting PageTurnerDelegate is nil. I tried a bunch of workarounds but all in vain. I do get the feeling that there may be a better way of doing this. Any suggestions or insights are most welcome!

推荐答案

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

You need to set the delegate property of the view controller instance that will be displayed:

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

Also, by convention, classes start with an upper case letter, so it should be UserInputViewController

这篇关于在 UIPageViewController 中以编程方式转到下一页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆