Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Swift Selector? Selector constantly results to `Type has no member`

I'm struggling with the following piece of code:

backgroundTimer = NSTimer.scheduledTimerWithTimeInterval(3, target: gvc, selector: #selector(GameViewController.addNext), userInfo: nil, repeats: true)

I can't seem to get the #selector working. This code results to:

Type 'GameViewController' has no member 'addNext'

Even though the member is right there... Here's the full code:

class GameViewController: GAITrackedViewController, AVAudioPlayerDelegate  {

    var sceneCanvas: SKSpriteNode?
    override func viewDidLoad() {
       skView.presentScene(WelcomeScene(size: view.bounds.size, gvc: self))
    }

    func createBackground(boundsSize: CGRect, gvc: GameViewController) -> SKSpriteNode {
       addUILabels(gvc)
       return sceneCanvas!
    }

    func addUILabels(gvc: GameViewController) {
        backgroundTimer = NSTimer.scheduledTimerWithTimeInterval(3, target: gvc, selector: #selector(GameViewController.addNext), userInfo: nil, repeats: true)
    }

    public func addNext() {
        let backgroundLabel = SKSpriteNode(imageNamed: "label1.png")
        sceneCanvas!.addChild(backgroundLabel!)
        backgroundLabel?.runAction(SKAction.moveByX(-screenSize.width , y: 0, duration: 12))
    }
}

class WelcomeScene: SKScene {
      init(size: CGSize, gvc: GameViewController){
        super.init ()
        let bounds = CGRect(origin: CGPoint(x: 0,y: 0), size: size)
        sceneCanvas = createBackground(bounds, gvc: gvc)
        self.addChild(sceneCanvas!)
    }
}
like image 266
user594883 Avatar asked May 02 '16 05:05

user594883


2 Answers

I was running into this issue with UILongPressGestureRecognizer and Swift 3.

I had my method in an extension for a long press on a table view cell:

// Long press table view extension
extension MyViewController: UIGestureRecognizerDelegate {

     public func handleLongPress(longPressGesture: UILongPressGestureRecognizer) {

         let p = longPressGesture.location(in: self.tableView)
         let indexPath = self.tableView.indexPathForRow(at: p)

         if indexPath == nil {
             print("Long press on table view, not row.")
         }
         else if (longPressGesture.state == UIGestureRecognizerState.began) {
             print("Long press on row, at \(indexPath!.row)")
         }   
     }
 }

Then, in my viewDidLoad():

     // Configure long press on tableviewcell
    let longPressGesture:UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(MyViewController.handleLongPress))
    longPressGesture.minimumPressDuration = 1.0 // 1 second press
    longPressGesture.delegate = self
    self.tableView.addGestureRecognizer(longPressGesture)

For me, the issue was fixed by using this syntax for the selector:

action: #selector(MyViewController.handleLongPress)

I tried the following alternatives without any luck. These do not work:

  • action: #selector(self.handleLongPress)
    
  • action: #selector(MyViewController.handleLongPress(_:))
    
  • action: #selector(self.handleLongPress(_:))
    
  • action: #selector("handleLongPress(_:)")
    

Additionally, even though I don't seem to be passing any arguments to MyViewController.handleLongPress, the selected row prints out to the console just fine on long press selection:

Long press on row, at 5

like image 101
thexande Avatar answered Oct 07 '22 02:10

thexande


backgroundTimer = NSTimer.scheduledTimerWithTimeInterval(3, target: self, selector: Selector("addNext"), userInfo: nil, repeats: true)
like image 44
Jayesh Miruliya Avatar answered Oct 07 '22 00:10

Jayesh Miruliya