No method declared with Objective-C selector ('nextPage') - ios

xcode creates some warnings since I updated to 7.3.1: "No method declared with Objective-C selector ('nextPage')". I'm not sure what I need to do now. These are the two lines that produce the warning:
let leftSwipe = UISwipeGestureRecognizer (target: self, action: Selector("nextPage"))
let rightSwipe = UISwipeGestureRecognizer (target: self, action: Selector("nextPage"))
This is the entire VC code:
import Foundation
import UIKit
class VC1 : UIViewController {
class MyCustomNavigationController: UINavigationController {
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return (visibleViewController?.supportedInterfaceOrientations())!
}
override func shouldAutorotate() -> Bool {
return (visibleViewController?.shouldAutorotate())!
}
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.Portrait
}
override func shouldAutorotate() -> Bool {
return true
}
override func viewDidLoad() {
super.viewDidLoad()
func nextPage(sender:UISwipeGestureRecognizer) {
switch sender.direction {
case UISwipeGestureRecognizerDirection.Left:
print("SWIPED LEFT", terminator: "")
self.performSegueWithIdentifier("seg1", sender: nil)
default:
break
}
let leftSwipe = UISwipeGestureRecognizer (target: self, action: Selector("nextPage"))
let rightSwipe = UISwipeGestureRecognizer (target: self, action: Selector("nextPage"))
leftSwipe.direction = .Left
rightSwipe.direction = .Right
view.addGestureRecognizer(leftSwipe)
view.addGestureRecognizer(rightSwipe)
}
}
}
I found this when researching:
Selector("funcName") was changed to #selector(ClassName.funcName)
I imagine that "funcName" would be nextPage, but what do I have to set as ClassName?
Unfortunately, xcode doesn't provide any suggestions to fix this. Hope you can help. Thanks.

You need to put nextPage out of viewDidLoad() and then #selector(VC1.nextPage)
func nextPage(sender:UISwipeGestureRecognizer) {
switch sender.direction {
case UISwipeGestureRecognizerDirection.Left:
print("SWIPED LEFT", terminator: "")
self.performSegueWithIdentifier("seg1", sender: nil)
default:
break
}
}
override func viewDidLoad() {
super.viewDidLoad()
let leftSwipe = UISwipeGestureRecognizer (target: self, action: #selector(VC1.nextPage))
let rightSwipe = UISwipeGestureRecognizer (target: self, action: #selector(VC1.nextPage))
}

Related

UISwipeGestureRecognizer on tvOS is giving me a EXC_BAD_ACCESS

I'm trying to add gesture code for swipe up/down to my UIViewController in a tvOS app.
override func loadView() {
let swipeDown = UISwipeGestureRecognizer(
target: self,
action: #selector(self.respondToSwipeGesture)
)
swipeDown.direction = UISwipeGestureRecognizer.Direction.down
self.view.addGestureRecognizer(swipeDown)
let swipeUp = UISwipeGestureRecognizer(
target: self,
action: #selector(self.respondToSwipeGesture)
)
swipeUp.direction = UISwipeGestureRecognizer.Direction.up
self.view.addGestureRecognizer(swipeUp)
}
#objc private func respondToSwipeGesture(gesture: UIGestureRecognizer) {
if let swipeGesture = gesture as? UISwipeGestureRecognizer {
switch swipeGesture.direction {
case UISwipeGestureRecognizer.Direction.down:
print("Swiped down")
case UISwipeGestureRecognizer.Direction.up:
print("Swiped up")
default:
break
}
}
}
When I run this I get a
Thread 1: EXC_BAD_ACCESS (code=2, address=0x7ffee7146fe8)
on the let swipeDown line in the loadView function.
I'm using Xcode 11. What am I missing here?
Yeah, missing super.loadView() in your code also, you should create the gestures in viewDidLoad instead loadView.
override func loadView() {
super.loadView() // ---> In this case always in the second line
let swipeDown = UISwipeGestureRecognizer(
target: self,
action: #selector(self.respondToSwipeGesture)
)
swipeDown.direction = UISwipeGestureRecognizer.Direction.down
self.view.addGestureRecognizer(swipeDown)
let swipeUp = UISwipeGestureRecognizer(
target: self,
action: #selector(self.respondToSwipeGesture)
)
swipeUp.direction = UISwipeGestureRecognizer.Direction.up
self.view.addGestureRecognizer(swipeUp)
}
#objc private func respondToSwipeGesture(gesture: UIGestureRecognizer) {
if let swipeGesture = gesture as? UISwipeGestureRecognizer {
switch swipeGesture.direction {
case UISwipeGestureRecognizer.Direction.down:
print("Swiped down")
case UISwipeGestureRecognizer.Direction.up:
print("Swiped up")
default:
break
}
}
}
You have overridden the wrong method:
override func loadView() {
You meant:
override func viewDidLoad() {

How to implement double tap for further action like for more information?

I have implemented a switch statement. Now I am struggling to add a double tap gesture to dispaly a more information depnding on the case chosen from the switch.
override func viewDidLoad() {
let tap = UITapGestureRecognizer(target: self, action: #selector(moreInfo))
tap.numberOfTapsRequired = 2
view.addGestureRecognizer(tap)
}
// the main switch case
switch choice {
case 1:
print("1 is chosen")
moreInfo(option:1)
case 2:
print("2 is chosen")
moreInfo(option:2)
}
//to be activated when double tap
#objc func moreInfo(option: Int) {
switch choice {
case 1:
print("more information for 1")
case 2:
print("more information for 2")
}
}
I just write some example code. Hope it help.
Like below code, you can use double and single tap actions.
class ViewController: UIViewController {
var singleTap: UITapGestureRecognizer!
var doubleTap: UITapGestureRecognizer!
override func viewDidLoad() {
super.viewDidLoad()
doubleTap = UITapGestureRecognizer(target: self, action: #selector(tap(gesture:)))
view.addGestureRecognizer(doubleTap)
doubleTap.numberOfTapsRequired = 2
doubleTap.delaysTouchesBegan = true
singleTap = UITapGestureRecognizer(target: self, action: #selector(tap(gesture:)))
singleTap.delaysTouchesBegan = true
singleTap.require(toFail: doubleTap)
view.addGestureRecognizer(singleTap)
}
#objc
func tap(gesture: UITapGestureRecognizer) {
switch gesture {
case singleTap:
moreInfo(option: 1)
case doubleTap:
moreInfo(option: 2)
default:
print("---")
}
}
func moreInfo(option: Int) {
switch option {
case 1:
print("more information for 1")
case 2:
print("more information for 2")
default:
print("---")
}
}
}

#selector in gesture Recognizer in xcode9

Hii everyone i'm just shifting from xcode8 to xcode9
and when i tryed to use gesture recognizer they xcode9 is showing some error
argument of #selector refers to instate method swipe(gesture) that is not >
expose to obj c
and here my code
override func viewDidLoad() {
super.viewDidLoad()
let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(swipe(gestuer:)))
swipeLeft.direction = .left
self.view.addGestureRecognizer(swipeLeft)
}
func swipe(gestuer: UISwipeGestureRecognizer) {
if gestuer.direction == .left {
print("this is left swipe")
}
}
so is it xcode problem of something else
You need to use the #objc attribute on swipe(gestuer:) to use it with #selector.
#objc func swipe(gestuer: UISwipeGestureRecognizer) {
if gestuer.direction == .left {
print("this is left swipe")
}
}

navigate between tab bar using swipe gesture

I want to navigate between my tab bar using swipe gestures. What is the easiest way to do that? I tried something like this...
import UIKit
class postAdViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
var leftSwipe = UISwipeGestureRecognizer(target: self, action: Selector("handleSwipes:"))
view.addGestureRecognizer(leftSwipe)
}
func handleSwipes(sender:UISwipeGestureRecognizer) {
if (sender.direction == .left) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "favourireviewcontroller") as! UIViewController
self.present(vc, animated: true, completion: nil)
}
if (sender.direction == .right) {
}
}
If I try to swipe right nothing happens. The app crashes when swiping left the following error message
unrecognized selector sent to instance 0x7f924380a730
Well if you want to navigate through you tabBar you should implement a swipeGestureRecognizer for .left and .right and then work with the tabBarController?.selectedIndex, something like this:
let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(swiped))
swipeRight.direction = UISwipeGestureRecognizerDirection.right
self.view.addGestureRecognizer(swipeRight)
let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(swiped))
swipeLeft.direction = UISwipeGestureRecognizerDirection.left
self.view.addGestureRecognizer(swipeLeft)
func swiped(_ gesture: UISwipeGestureRecognizer) {
if gesture.direction == .left {
if (self.tabBarController?.selectedIndex)! < 2 { // set your total tabs here
self.tabBarController?.selectedIndex += 1
}
} else if gesture.direction == .right {
if (self.tabBarController?.selectedIndex)! > 0 {
self.tabBarController?.selectedIndex -= 1
}
}
}
Here's a Swift4 example of swiping left and right right through a TabBar controller.
Things I don't like about this solution:
- seems like you should be able to register the handler once and distinguish the direction within the handler itself but I wasn't able to easily do that.
- I'm also curious how to do a gesture that includes a drag for visual effect. I'm think I need to include a PanGesture as well in order to pull that off.
override func viewDidLoad() {
super.viewDidLoad()
let leftSwipe = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipes(_:)))
let rightSwipe = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipes(_:)))
leftSwipe.direction = .left
rightSwipe.direction = .right
self.view.addGestureRecognizer(leftSwipe)
self.view.addGestureRecognizer(rightSwipe)
}
And then the handler:
#objc func handleSwipes(_ sender:UISwipeGestureRecognizer) {
if sender.direction == .left {
self.tabBarController!.selectedIndex += 1
}
if sender.direction == .right {
self.tabBarController!.selectedIndex -= 1
}
}
Here is a slightly modified version of an earlier suggestion that is ready for Swift 5 and iOS 12.
The real advantages are:
it uses guard statements so you don't have to mess with unwrapping the tabBarController and the viewControllers in the handler
it doesn't require a hard-coded number of tabs — it just gets it from the viewControllers array.
I also cleaned it up a bit to make it a little less verbose. It has been tested with Swift 5 running on iOS 12 (and it should be fine on iOS 11.4, since that was my minimum deployment version that this was tested on).
Add these to viewDidLoad (or other appropriate location of your choosing):
let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipeGesture))
swipeRight.direction = .right
self.view.addGestureRecognizer(swipeRight)
let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipeGesture))
swipeLeft.direction = .left
self.view.addGestureRecognizer(swipeLeft)
Then add this as your handler for the events:
#objc func handleSwipeGesture(_ gesture: UISwipeGestureRecognizer) {
guard let tabBarController = tabBarController, let viewControllers = tabBarController.viewControllers else { return }
let tabs = viewControllers.count
if gesture.direction == .left {
if (tabBarController.selectedIndex) < tabs {
tabBarController.selectedIndex += 1
}
} else if gesture.direction == .right {
if (tabBarController.selectedIndex) > 0 {
tabBarController.selectedIndex -= 1
}
}
}
try using the Swift 4 selector syntax:
//below code write in view did appear()
let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(swiped))
swipeRight.direction = UISwipeGestureRecognizerDirection.right
self.view.addGestureRecognizer(swipeRight)
let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(swiped))
swipeLeft.direction = UISwipeGestureRecognizerDirection.left
self.view.addGestureRecognizer(swipeLeft)
// below code create swipe gestures function
// MARK: - swiped
#objc func swiped(_ gesture: UISwipeGestureRecognizer) {
if gesture.direction == .left {
if (self.tabBarController?.selectedIndex)! < 2
{ // set here your total tabs
self.tabBarController?.selectedIndex += 1
}
} else if gesture.direction == .right {
if (self.tabBarController?.selectedIndex)! > 0 {
self.tabBarController?.selectedIndex -= 1
}
}
}
Try using the Swift 3 selector syntax:
let leftSwipe = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipes(_:))
like so
override func viewDidLoad() {
super.viewDidLoad()
nextButton.layer.cornerRadius = 7
let leftSwipe = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipes(_:))
//leftSwipe.direction = .right
view.addGestureRecognizer(leftSwipe)
}
func handleSwipes(_ sender: UISwipeGestureRecognizer) {
if (sender.direction == .left) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "favourireviewcontroller") as! UIViewController
self.present(vc, animated: true, completion: nil)
}
if (sender.direction == .right) {
}
}
Swift 3 introduced this feature to enable the compiler to check whether the function you're specifying actually exists. It is therefore much saver than the concepts before.

How do I create a UISwipeGestureRecognizer programatically

private let swipeUp: UISwipeGestureRecognizer = {
let swiper = UISwipeGestureRecognizer(target: self, action: #selector(movedUp))
swiper.direction = .up
return swiper
}()
private let swipeDown: UISwipeGestureRecognizer = {
let swiper = UISwipeGestureRecognizer(target: self, action: #selector(movedDown))
swiper.direction = .down
return swiper
}()
creating my swipeUp and swipeDown gesture recognizers
func movedUp(sender: UISwipeGestureRecognizer){
print("UP")
}
func movedDown(sender: UISwipeGestureRecognizer){
print("DOWN")
}
my functions to be called when the swipe is recieved
override func viewDidLoad() {
self.view.addGestureRecognizer(swipeUp)
self.view.addGestureRecognizer(swipeDown)
}
adding my gesture recognizers in viewDidLoad
my program runs, but nothing happens when i swipe, what am i doing wrong?
Thankyou everyone
Try this:
private lazy var swipeUp: UISwipeGestureRecognizer = {
let swiper = UISwipeGestureRecognizer(target: self, action: #selector(movedUp))
swiper.direction = .up
return swiper
}()
private lazy var swipeDown: UISwipeGestureRecognizer = {
let swiper = UISwipeGestureRecognizer(target: self, action: #selector(movedDown))
swiper.direction = .down
return swiper
}()
override func viewDidLoad()
{
super.viewDidLoad()
self.view.addGestureRecognizer(swipeUp)
self.view.addGestureRecognizer(swipeDown)
}
func movedUp(sender: UISwipeGestureRecognizer)
{
print("UP")
}
func movedDown(sender: UISwipeGestureRecognizer)
{
print("DOWN")
}
Have you tried func movedUp(sender: UIGestureRecognizer)

Resources