简体   繁体   中英

Selected segment of a segment controller should change when swiping a page view controller

I am designing a segment control in which I have I have implemented a page view controller to swipe in between views. And also I have implemented segment action functionality to switch between views when user touches any particular segment.

But the problem I am facing now is whenever I select a particular segment by clicking on the segment the selected segment changes but whenever I am swiping in between views I am unable to change selected index.

So can't I get every time the index of the view that loads ? Or any suggestion to my problem .

I have implemented the following functions .

 func viewControllerAtIndex(index : Int) -> UIViewController/
    {

    }
    func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController?
    {

    }


    func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController?
    {


    }

    @IBAction func sendsegmentcontrollerIndex(sender: AnyObject)

    {

  //codes for changing the present view controller on segment selection

    }

So what I must to do now in order to get my expected result? I even tried to add swipe gesture to my page view controller but in vain.

You should override this delegate: pageViewController(pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) . Maybe you can implement like this:

func pageViewController(pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
    if pageViewController.viewControllers?.first?.isKindOfClass([yourClass]) {
        //self.changeIndex()
    }
}

In this delegate you will detect what is page moved. And you will set your segment to index match with this viewcontroller .

func pageViewController(pageViewController: UIPageViewController,
        didFinishAnimating finished: Bool,
        previousViewControllers: [UIViewController],
        transitionCompleted completed: Bool)
    {
        let pageContentViewController = pageViewController.viewControllers![0] as! ContentViewController
        let index = pageContentViewController.pageIndex
        print(index)
        seg.selectedSegmentIndex = index



    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM