在Swift的UIPageViewController中,视图未显示为“ setViewController”

时间:2019-02-19 10:58:24

标签: ios swift xcode uipageviewcontroller

我想以编程方式更改UIPageView显示的页面。

我创建了两个打算在PageView中显示的ViewController,并在viewDidLoad中使用“ setViewControllers”设置了初始View。我正在工作,显示了视图,并且可以在屏幕上滑动以在两个视图之间切换。

我的问题是,当我单击按钮时尝试再次调用“ setViewControllers”来更改显示的视图时,什么也没发生。

我正在使用委托,因为这些按钮是另一个ViewController的一部分,并使用NSLog来确保调用了函数(这种情况)。

如果有人可以帮助我理解为什么“ setViewControllers”方法什么都不做,那就太好了。

这是我的 UIPageViewController 的代码:

import UIKit

class FrontContentController: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate, sideMenuDelegate {

    func presentationCount(for pageViewController: UIPageViewController) -> Int {
        return subViewControllers.count
    }

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
        let currentIndex:Int = subViewControllers.index(of: viewController) ?? 0
        if currentIndex <= 0 {
            return nil
        }
        return subViewControllers[currentIndex - 1]
    }

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
        let currentIndex:Int = subViewControllers.index(of: viewController) ?? 0
        if currentIndex >= subViewControllers.count - 1 {
            return nil
        }
        return subViewControllers[currentIndex + 1]
    }

    lazy var subViewControllers:[UIViewController] = {
        return [
            UIStoryboard(name: "frontPage", bundle: nil).instantiateViewController(withIdentifier: "FirstContent") as! FirstContentController,
            UIStoryboard(name: "frontPage", bundle: nil).instantiateViewController(withIdentifier: "SecondContent") as! SecondContentController
        ]
    }()

    var sideMenuVC : SideMenuController!

    override func viewDidLoad() {
        super.viewDidLoad()
        self.delegate = self
        self.dataSource = self
        self.sideMenuVC = (self.storyboard?.instantiateViewController(withIdentifier: "SideMenu") as! SideMenuController)
        self.setViewControllers([subViewControllers[0]], direction: .forward, animated: true, completion: nil)
        // Do any additional setup after loading the view.
    }

    func changeColor(_ color: UIColor) {
        if color == .blue {
            self.setViewControllers([subViewControllers[0]], direction: .forward, animated: false, completion: nil)
            NSLog("Blue Delegate")
        }
        if color == .red {
            self.setViewControllers([subViewControllers[1]], direction: .forward, animated: false, completion: nil)
            NSLog("Red Delegate")
        }
    }
}

并且如果相关,用按钮(侧面菜单)单独的 UIViewController

import UIKit

protocol sideMenuDelegate: AnyObject {
    func changeColor(_ color : UIColor)
}

class SideMenuController: UIViewController {

    weak var delegate : sideMenuDelegate?
    var frontPageVC : FrontPageController!
    var frontContentPC : FrontContentController!

    override func viewDidLoad() {
        super.viewDidLoad()
        let storyboard = UIStoryboard(name: "frontPage", bundle: nil)
        frontPageVC = (storyboard.instantiateViewController(withIdentifier: "FrontPage") as! FrontPageController)
        frontContentPC = (storyboard.instantiateViewController(withIdentifier: "FrontContent") as! FrontContentController)
        self.delegate = frontContentPC
        self.view.backgroundColor = UIColor.black
    }

    @IBAction func pressBlueButton(_ sender: Any) {
        delegate?.changeColor(UIColor .blue)
    }
    @IBAction func pressRedButton(_ sender: Any) {
        delegate?.changeColor(UIColor .red)
    }
}

谢谢

0 个答案:

没有答案