Reputation: 1351
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!)
}
}
Upvotes: 14
Views: 2578
Reputation: 1760
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
Upvotes: 8
Reputation: 1351
Turns out that two things were the problem. 1) I had defined the method outside the class, 2) However also the following syntax was needed for it work:
selector: #selector(addNext as () -> ())
Upvotes: 1
Reputation: 3012
Have you tried this.
backgroundTimer = NSTimer.scheduledTimerWithTimeInterval(3, target: self, selector: #selector(GameViewController.addNext), userInfo: nil, repeats: true)
or
backgroundTimer = NSTimer.scheduledTimerWithTimeInterval(3, target: gvc, selector: #selector(gvc.addNext), userInfo: nil, repeats: true)
Upvotes: 2
Reputation: 4532
Probably your code is in different modules. If so you should make your func addNext()
public:
public func addNext() {
...
}
Upvotes: 2
Reputation: 3317
backgroundTimer = NSTimer.scheduledTimerWithTimeInterval(3, target: self, selector: Selector("addNext"), userInfo: nil, repeats: true)
Upvotes: 2