1

I'm using a library that manages the swiping between view controllers.

It seems like the only way that I can use a button in one of my view controller to navigate to the other is by calling a specific method from the view controller that manages the swiping feature.

It there any way to call a method in a different view controller?

Thank you

Walking
  • 467
  • 1
  • 7
  • 23
  • what's the library ? – the_critic Feb 13 '16 at 01:12
  • EZSwipeController. It seems like they have default functionality for navigation controller buttons but I just want to use a regular button. – Walking Feb 13 '16 at 01:13
  • Could you elaborate on what exactly you are trying to achieve ? "Call code from another view controller" seems a little ambiguous to me... More specifics would be nice – the_critic Feb 13 '16 at 01:20
  • There's a piece of code in the library swift file that will navigate to the left view controller or to the right. i'm hoping that if from my main view controller I can set the action of a button to the action in that library that moves to the left view controller, that the button will navigate me there. It uses a pageviewcontroller implementation. Would I be able to navigate to the view controller on the left or right an easier way? Thank you – Walking Feb 13 '16 at 01:23

2 Answers2

0

There are many ways to communicate between view controllers. One of the most common patterns for doing so on iOS is the delegate pattern. One view controller holds a reference to the other and the delegate conforms to a protocol. The protocol is a set of methods that the first view controller can call when specific events happen. I would try setting up a protocol and have one of your view controllers be a delegate to the other

Kyle Redfearn
  • 2,172
  • 16
  • 34
  • Thank for your response. Would you be able to point me to a resource? That all sounded a bit familiar but the implementation seems intimidating. Thank you – Walking Feb 13 '16 at 03:44
  • I added two links in there for the delegate pattern and protocol. See the "Delegation" section on this site: http://www.raywenderlich.com/46988/ios-design-patterns – Kyle Redfearn Feb 13 '16 at 03:45
0

If you take a look at the code that is hosted on GitHub you can see that the page controller is exposed from the EZSwipeController class. So given that you subclass EZSwipeController (or maintain a reference somewhere), you can now access the pageViewController property of it and scroll to a given page.

If you subclass:

class MyVC : EZSwipeController{
   func buttonTapped(){ /* use self.pageViewController to "page" */ }
}

Weirdly, I have never personally worked with UIPageViewController, and as far as I can tell there is no easy way to scroll to a page in an easy fashion.

I haven't personally tried it (I usually validate my answers before posting), but you should be able to pull it off. If you don't have a reference to the view controller you need to get it (let me know if that is the core issue).

There is a question on SO that seems to enjoy some popularity regarding UIPageViewController paging:

UIPageViewController, how do I correctly jump to a specific page without messing up the order specified by the data source?

And from Apple docs:

enter image description here

I would also encourage you to look at the code inside of the EZSwipeController repo, where you can find a non-exposed method that does the scrolling:

@objc private func clickedLeftButton() {
        let currentIndex = stackPageVC.indexOf(currentStackVC)!
        datasource?.clickedLeftButtonFromPageIndex?(currentIndex)

        let shouldDisableSwipe = datasource?.disableSwipingForLeftButtonAtPageIndex?(currentIndex) ?? false
        if shouldDisableSwipe {
            return
        }

        if currentStackVC == stackPageVC.first {
            return
        }
        currentStackVC = stackPageVC[currentIndex - 1]
        pageViewController.setViewControllers([currentStackVC], direction: UIPageViewControllerNavigationDirection.Reverse, animated: true, completion: nil)
    }

The "key" line is this one:

pageViewController.setViewControllers([currentStackVC], direction: UIPageViewControllerNavigationDirection.Reverse, animated: true, completion: nil)

I think EZSwipeController also exposes the view controllers contained in the pageViewController which is a property called stackVC, which is an array of the view controllers contained in the page view controller.

I assume with this knowledge you should be able to page to a given page, despite seeming a little "hacky" (IMHO the developers should have exposed paging logic from the get-go).

My advice for you after all this is simple:

Try to roll it yourself. This is not a huge undertaking and you maintain full control over what's accessible and how you want to work with it. The EZSwipeController class is quite small, so there is not a whole lot of code you'd have to write for your own solution. You could also go ahead and fork the repo and modify/use it to your liking.

I hope that helps. If you have any further questions or if something is unclear, I'd be ready to help you out.

Community
  • 1
  • 1
the_critic
  • 12,720
  • 19
  • 67
  • 115