UIPageViewController没有使用setViewControllers

时间:2018-01-29 00:37:51

标签: ios swift uikit uipageviewcontroller

我有一个带有两个UIViewControllers作为子节点的UIPageViewController。我希望用户能够在子视图之间进行分页,但我也希望能够自动调用页面。

我已经尝试过使用setViewControllers的每个排列,但是没有一个能够在程序控制下推进页面。

我觉得我必须遗漏一些基本的东西,但我看不到它。

这是代码。

    import UIKit
    import Alamofire
    import SwiftyJSON
    import SVProgressHUD

    class auiPrefsPageViewController: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {

        lazy var auiPrefsControllers: [UIViewController] = {
            return [self.auiPrefsControllerInstance(name: "auiURLPrefs"), self.auiPrefsControllerInstance(name: "auiCredentialsPrefs")]

        }()

        private func auiPrefsControllerInstance(name: String) -> UIViewController {
            return UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: name)

        }

        public func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
            guard let viewControllerIndex = auiPrefsControllers.index(of: viewController) else {
                return nil

            }

            let previousIndex = viewControllerIndex - 1

            guard previousIndex >= 0 else {
                return nil

            }

            guard auiPrefsControllers.count > previousIndex else {
                return nil
            }

            return auiPrefsControllers[previousIndex]

        }

        public func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
            guard let viewControllerIndex = auiPrefsControllers.index(of: viewController) else {
                return nil

            }

            let nextIndex = viewControllerIndex + 1

            guard nextIndex < auiPrefsControllers.count else {
                return nil

            }

            guard auiPrefsControllers.count > nextIndex else {
                return nil

            }

            return auiPrefsControllers[nextIndex]

        }

        public func presentationCount(for pageViewController: UIPageViewController) -> Int {
            return auiPrefsControllers.count

        }

        public func presentationIndex(for pageViewController: UIPageViewController) -> Int {
            guard let firstViewController = viewControllers?.first, let firstViewControllerIndex = auiPrefsControllers.index(of: firstViewController) else {
                return 0
            }

            return firstViewControllerIndex

        }

        override func viewDidLoad() {
            super.viewDidLoad()
            self.dataSource = self
            self.delegate = self

            if let vc1 = auiPrefsControllers.first {
                setViewControllers([vc1], direction: .forward, animated: true, completion: nil)
            }

        }

        override func viewDidLayoutSubviews() {
            super.viewDidLayoutSubviews()
            for view in self.view.subviews {
                if view is UIScrollView {
                    view.frame = UIScreen.main.bounds
                } else {
                    view.backgroundColor = UIColor.clear

                }

            }

        }

        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }


        func auiPrefsGoNext() {
            let controller = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "auiCredentialsPrefs")
            setViewControllers([controller], direction: .forward, animated: true, completion: nil)
        }

        public func obtainPrimaryURL(mCode: String) {
            SVProgressHUD.show()
            let rString = "ImaURLthatcantbepublic?aparam=" + mCode

            Alamofire.request(rString).responseJSON { response in
                if let result = response.result.value {
                    SVProgressHUD.dismiss()
                    let json = JSON(result)
                    if(json["message"] == "ok") {
                        let mURL: String = json["awhURL"].string!
                        let mAPI: String = json["apiURL"].string!

                        // save URLs
                        let sState = auiCoreDataHandler.saveBaseURL(baseURL: mURL)
                        if (!sState) {
                            let sState = auiCoreDataHandler.saveBaseURL(baseURL: "")

                        }

                        self.auiPrefsGoNext()

                    } else {
                        // handle a return error
                        SVProgressHUD.dismiss()

                        // some actual error handling here

                    }

                }

            }

        }

从这里可以看出,两个重要的位是函数obtainPrimaryURL和auiPrefsGoNext。第一个出去并获取一些数据并存储它,然后它通过调用auiPrefsGoNext尝试前进到下一页。就在这里,我一直在wra my brain brain and and and。。。。。。。底线是它没有抛出任何错误,但它没有进入下一页。

1 个答案:

答案 0 :(得分:0)

看起来您已经创建了auiCredentialsPrefs控制器一次(在lazy var中),为什么要在auiPrefsGoNext中创建第二个(和每个)时间?

如果这是我的代码,我会这样做:

func auiPrefsGoNext() {
    let auiCredentialsPrefsController = auiPrefsControllers[1]
    setViewControllers([auiCredentialsPrefsController], direction: .forward, 
                       animated: true, completion: nil)
}

另外,我没有看到你在lazy var做这件事的好处。