Reputation: 163
self.presentViewController(playerViewController, animated: true){
self.playerViewController.player?.play()
self.playerTimer = NSTimer.scheduledTimerWithTimeInterval(4, target: self, selector:Selector("stopAfter4seconds:"), userInfo: nil, repeats: false)
}
func stopAfter4seconds(timer: NSTimer){
self.playerViewController.player?.pause()
self.playerViewController.player = nil
self.dismissViewControllerAnimated(true, completion: nil)
self.presentViewController(GameViewController1(), animated: true, completion: nil)
}
}
So.. I've tried just about everything to get this NSTimer to work and nothing seems to go correctly. I've tried:
Adding & Removing colon in between
("stopAfter4seconds:")
Adding & Removing the (timer:NSTimer) in the parameter of the
stopAfter4seconds()
Adding & Removing the second Selector in
selector:Selector("stopAfter4seconds:")
Errors I'm getting:
stopAfter4seconds]: unrecognized selector sent to instance 0x7bb1ae90
Terminating app due to uncaught exception 'NSInvalidArgumentException'
Thread 1 : signal SIGABRT
I have no idea what I'm doing wrong so please if anyone has any idea let me know.
Upvotes: 2
Views: 106
Reputation: 437542
If the method is defined like so:
func stopAfter4seconds(timer: NSTimer) { ... }
Then you'd create the timer like so:
NSTimer.scheduledTimerWithTimeInterval(4, target: self, selector: Selector("stopAfter4seconds:"), userInfo: nil, repeats: false)
Or you can omit Selector()
and just do:
NSTimer.scheduledTimerWithTimeInterval(4, target: self, selector: "stopAfter4seconds:", userInfo: nil, repeats: false)
It would appear, though, that you've implemented this stopAfter4seconds
function inside the method that called presentViewController
. This stopAfter4seconds
, itself, must be a method, not defined inside another method.
Upvotes: 1
Reputation: 8832
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(animated: Bool) {
print("run viewDidAppear")
myFunction1()
}
func myFunction1() {
print("call timer")
NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: Selector("myFunction2"), userInfo: nil, repeats: false)
}
func myFunction2(){
print("do stuff")
}
}
Upvotes: 1