从pageViewController

时间:2016-05-24 12:25:39

标签: ios swift

我有一个基于页面的应用程序,由每个ViewController中的collectionView组成。然后我在pageView上的barButton上有一个viewController,它导致一个添加页面供用户添加他们的数据。我试图找到一种方法来确定源视图控制器是什么,以便我可以追加并重新加载正确的ViewController。下面是我的UIPageView和AddSubject类:

AddSubject.swift:

import UIKit
import ChameleonFramework


class AddSubjectView: UIViewController {

    // collectionView
    @IBOutlet var colourPicker: UICollectionView!

    var source = UIViewController()

    // source of colours to use in the colour collectionView
    private var data = collectionViewColors.createColor()

    override func viewDidLoad() {
        super.viewDidLoad()

        // gesture recognizer to determine when the user has tapped anywhere but the text field

        let tap: UIGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(AddSubjectView.dismissKeyboard))
        view.addGestureRecognizer(tap)


    }

    @IBAction func addPressed(sender: AnyObject) {




    }

    func addData() {




    }

    // dismiss keyboard when the user taps away from the text field (called above with #selector)
    func dismissKeyboard() {

        view.endEditing(true)
    }

// mandatory methods to display collectionView data in the collectionView
}
extension AddSubjectView: UICollectionViewDataSource {

    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 1
    }

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        return data.count
    }

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! colourCell



        cell.data = self.data[indexPath.item]


        return cell
    }



    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

        let cell = collectionView.cellForItemAtIndexPath(indexPath)

    }



}

PageViewController.swift:

import UIKit
import ChameleonFramework

class PageViewController: UIPageViewController,UIPageViewControllerDelegate {

    // created for use in the function which determines the title of the navigationBar
    var arrayIndex: Int = 0
    // reference for later function to determine title of navigationBar
    var pageControl = UIPageControl.self

    // NSUserDefaults
    let defaults = NSUserDefaults.standardUserDefaults()

    // set of viewControllers (based on their storyboard ID)

    private(set) lazy var orderedViewControllers: [UIViewController] = {
        return [self.newDayViewController("monday"),
                self.newDayViewController("tuesday"),
                self.newDayViewController("wednesday"),
                self.newDayViewController("thursday"),
                self.newDayViewController("friday"),
                self.newDayViewController("saturday"),
                self.newDayViewController("sunday")
        ]
    }()


    override func viewDidLoad() {
        super.viewDidLoad()


        self.setNeedsStatusBarAppearanceUpdate()
        // setting the datasource & Delegate of the UIPageViewController
        dataSource = self
        delegate = self




        // set the first viewController for the pageView (monday as it is the first in the set)

        if let firstViewController = orderedViewControllers.first {
            setViewControllers([firstViewController],
                               direction: .Forward,
                               animated: true,
                               completion: nil)


        }

    }





    // function to add view controllers
    // function will only instantiateViewControllers which have a storyboard id containing 'day' e.g Monday, Tuesday, Wednesday

    private func newDayViewController(day: String) -> UIViewController {
        return UIStoryboard(name: "Main", bundle: nil) .
            instantiateViewControllerWithIdentifier("\(day)")
    }




    // set the navigationBar title to the dayViewController's title
    // if the user is looking at orderedViewControllers[1], then the title of that day is "Tuesday"

    func pageViewController(pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
        if (!completed) {
            return
        }


        if let firstViewController = viewControllers?.first,
            let arrayIndex = orderedViewControllers.indexOf(firstViewController) {
            switch arrayIndex {
            case 0:
                self.navigationController!.navigationBar.topItem!.title = "Monday"
                self.navigationItem.title = "Monday"
                break

            case 1:
                self.navigationController!.navigationBar.topItem!.title = "Tuesday"
                self.navigationItem.title = "Tuesday"
                break

            case 2:
                self.navigationController!.navigationBar.topItem!.title = "Wednesday"
                self.navigationItem.title = "Wednesday"
                break

            case 3:
                self.navigationController!.navigationBar.topItem!.title = "Thursday"
                break
            case 4:
                self.navigationController!.navigationBar.topItem!.title = "Friday"
                break

            case 5:
                self.navigationController!.navigationBar.topItem!.title = "Saturday"
                break

            case 6:
                self.navigationController!.navigationBar.topItem!.title = "Sunday"
                break

            default:
                self.navigationController!.navigationBar.topItem!.title = "Timetable"


            }
        }
    }
}
// Mandatory functions and setup for the pageViewController to work

extension PageViewController: UIPageViewControllerDataSource {


    func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
        guard let viewControllerIndex = orderedViewControllers.indexOf(viewController) else {
            return nil
        }
        let previousIndex = viewControllerIndex - 1
        guard previousIndex >= 0 else {
            return orderedViewControllers.last
        }
        guard orderedViewControllers.count > previousIndex else {
            return nil
        }
        return orderedViewControllers[previousIndex]
    }

    func pageViewController(pageViewController: UIPageViewController,
                            viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
        guard let viewControllerIndex = orderedViewControllers.indexOf(viewController) else {
            return nil
        }
        let nextIndex = viewControllerIndex + 1
        let orderedViewControllersCount = orderedViewControllers.count


        guard orderedViewControllersCount != nextIndex else {
            return orderedViewControllers.first
        }
        guard orderedViewControllersCount > nextIndex else {
            return nil
        }
        return orderedViewControllers[nextIndex]
    }
    // set number of viewControllers to be presented
    func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int {
        return orderedViewControllers.count
    }
    func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int {
        guard let firstViewController = viewControllers?.first,
            firstViewControllerIndex = orderedViewControllers.indexOf(firstViewController) else {
                return 0
        }
        return firstViewControllerIndex
    }
}

我想知道是否有办法使用一个addSubject视图,而不必为所有单独的viewControllers创建barButton项目,并将它们链接到各自的viewController。

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:1)

要在UIPageViewController中获取屏幕上的当前UIViewController,您可以实现UIPageViewControllerDelegate协议方法“didFinishAnimating”。我在我的一个应用程序中使用以下示例来实现此目的。

dialog.Close();

如果您需要索引,最简单的方法是创建自己的UIViewController子类,并在其上添加索引属性。然后在你的“viewControllerBeforeViewController”和“viewControllerAfterViewController”方法中,可以在返回ViewController之前分配索引

示例:

 func pageViewController(pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {

    let currentVC = pageViewController
        .viewControllers![pageViewController.viewControllers!.count - 1]
}