如何使用Swift选择器?选择器经常导致`类型没有成员`

use*_*883 14 selector nstimer ios sprite-kit swift

我正在努力使用以下代码:

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

我似乎无法让#selector工作.此代码的结果是:

Type 'GameViewController' has no member 'addNext'
Run Code Online (Sandbox Code Playgroud)

即使会员就在那里......这是完整的代码:

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!)
    }
}
Run Code Online (Sandbox Code Playgroud)

the*_*nde 8

我UILongPressGestureRecognizer和Swift 3 一起遇到了这个问题.

我在扩展中使用了我的方法来长按表格视图单元格:

// 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)")
         }   
     }
 }
Run Code Online (Sandbox Code Playgroud)

然后,在我的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)
Run Code Online (Sandbox Code Playgroud)

对我来说,通过对选择器使用此语法来解决该问题:

action: #selector(MyViewController.handleLongPress)
Run Code Online (Sandbox Code Playgroud)

没有任何运气,我尝试了以下替代品.这些不起作用:

另外,即使我似乎没有传递任何参数MyViewController.handleLongPress,所选行也会在长按选择时打印到控制台:

Long press on row, at 5


use*_*883 1

事实证明,有两件事是问题所在。1)我在类之外定义了该方法,2)但是它的工作还需要以下语法:

selector: #selector(addNext as () -> ())