bharath gangupalli
bharath gangupalli

Reputation: 550

Add Swipe Gesture to all View controllers in the project

I have set of 50 view controllers with back button. Now I need to add Left to Right swipe gesture for all view controllers in the project. When a swipe is triggered, I need to call back btn clicked event for respective view controller. It is difficult to add swipe gesture to each view controller. Is there any other way to do it with minimal changes to view controllers. Thanks in advance.

Regards, Bharath G

Upvotes: 1

Views: 949

Answers (3)

Zigii Wong
Zigii Wong

Reputation: 7826

Create a YourViewController class and implement your SwipeGesture method in viewDidLoad method.

In your 50 viewcontrollers .h file:

@interface ViewController : YourViewController

Let all your 50 viewControllers inherit from your YourViewController.

Upvotes: 2

Jef
Jef

Reputation: 4728

Well these other guys have told you why you may not need to do this, but assuming you do (not using UINavigationController || supporting pre iOs7 ..) then surely the easiest way to add to all of them would be to make a new UIViewController subclass, add the swipeRecogniser in viewDidLoad or similar, and then modify each of your 50 other controllers so that they all inherit from this one (i.e. abstract superclass..)

I would always have an abstract superclass above UIViewController in a project with 50 or so, obviously there is plenty of code that can be shared

Upvotes: 3

vijeesh
vijeesh

Reputation: 1327

It should work automatically if the back button is visible. If you are displaying a leftBarButtonItem instead of the back button, the gesture will not be present by default. Also, if you are using a UINavigationBar but not a UINavigationController, you won't see this functionality.

If you are using a UINavigationController and your view controller's navigation item contains a leftBarButtonItem, it's still possible to add functionality for the swipe left to right gesture of the navigation controller, by attaching a delegate to the navigation controller's interactivePopGestureRecognizer.

Note: The Swipe to Back feature is introduced with iOS 7(i Hope you are Developing App fos iOS 7 or later)

Upvotes: 0

Related Questions