简体   繁体   English

滑动页面视图控制器时,段控制器的选定段应更改

[英]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) . 您应该重写此委托: 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 . 然后,您将使用此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



    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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