UIPageControl不更新,即使值已更改

时间:2018-08-09 08:13:23

标签: ios swift xcode swift3 swift4

因此,我有 MainViewController ContainerView ,其中嵌入了PageViewController这个PageViewController有2个页面,我在containerView的顶部添加了一个PageControl,但是尝试使用方法更新页面

 PageControl.currentPage = status.page

不起作用。另外,我最初打印了该值,并且在刷卡后,当前页面的值发生了变化,但是用户界面未更新。

这是我到目前为止所做的。

MainViewController

   struct status {
    static var viewstat = ""
    static var bottomContainerHeightCon: CGFloat!
    static var containerHeightCon: CGFloat!
    static var bhbefore: CGFloat!
    static var chbefore: CGFloat!
    static var buttonstat = "false"
    static var page: Int = 0
}
   @IBOutlet var pageControl: UIPageControl!


    override func viewDidLoad() {
    super.viewDidLoad()

    }


  func changePage() {

    view.layoutIfNeeded()
}


   func configurePageControl() {
    // The total number of pages that are available is based on how many available colors we have.

    self.pageControl.numberOfPages = 2
    self.pageControl.currentPage = status.page
    self.pageControl.tintColor = UIColor.gray
    self.pageControl.pageIndicatorTintColor = UIColor.gray
    self.pageControl.currentPageIndicatorTintColor = UIColor.black   
}

PageViewController

  override func viewDidLoad() {
    super.viewDidLoad()

    self.delegate = self
    self.dataSource = self
     configurePageControl()
    let page1: UIViewController! = storyboard?.instantiateViewController(withIdentifier: "QuestionsPage");
    let page2: UIViewController! = storyboard?.instantiateViewController(withIdentifier: "NeedPaymentPage");
    pages.append(page1)
    pages.append(page2)
    setViewControllers([page1], direction: UIPageViewControllerNavigationDirection.forward, animated: false, completion: nil)


}


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


    let pageContentViewController = pageViewController.viewControllers![0]
    self.pageControl.currentPage = pages.index(of: pageContentViewController)!


  let currentIndex = pages.index(of: previousViewControllers.first!)!
  NeedDetailsController.status.page = currentIndex
    NeedController.changePage()


}

1 个答案:

答案 0 :(得分:0)

问题是这些行:

NeedDetailsController.status.page = currentIndex
NeedController.changePage()

您不能设置这样的值。尝试在didFinishAnimating方法中添加此if块,然后使用parentController访问属性。

if let parentController = self.parent as? NeedDetailsController {
    print("didFinishAnimating is getting called.")
}